xref: /dflybsd-src/sys/dev/drm/i915/intel_i2c.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1bad0eccaSFrançois Tigeot /*
2bad0eccaSFrançois Tigeot  * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
3bad0eccaSFrançois Tigeot  * Copyright © 2006-2008,2010 Intel Corporation
4bad0eccaSFrançois Tigeot  *   Jesse Barnes <jesse.barnes@intel.com>
5bad0eccaSFrançois Tigeot  *
6bad0eccaSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
7bad0eccaSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
8bad0eccaSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
9bad0eccaSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bad0eccaSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
11bad0eccaSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
12bad0eccaSFrançois Tigeot  *
13bad0eccaSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
14bad0eccaSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
15bad0eccaSFrançois Tigeot  * Software.
16bad0eccaSFrançois Tigeot  *
17bad0eccaSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18bad0eccaSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19bad0eccaSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20bad0eccaSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21bad0eccaSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22bad0eccaSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23bad0eccaSFrançois Tigeot  * DEALINGS IN THE SOFTWARE.
24bad0eccaSFrançois Tigeot  *
25bad0eccaSFrançois Tigeot  * Authors:
26bad0eccaSFrançois Tigeot  *	Eric Anholt <eric@anholt.net>
27bad0eccaSFrançois Tigeot  *	Chris Wilson <chris@chris-wilson.co.uk>
28bad0eccaSFrançois Tigeot  */
29a2fdbec6SFrançois Tigeot #include <linux/i2c.h>
309f4ca867SFrançois Tigeot #include <linux/i2c-algo-bit.h>
31a2fdbec6SFrançois Tigeot #include <linux/export.h>
32bad0eccaSFrançois Tigeot #include <drm/drmP.h>
33a2fdbec6SFrançois Tigeot #include "intel_drv.h"
34bad0eccaSFrançois Tigeot #include <drm/i915_drm.h>
35bad0eccaSFrançois Tigeot #include "i915_drv.h"
36a2fdbec6SFrançois Tigeot 
3719c468b4SFrançois Tigeot struct gmbus_pin {
38a2fdbec6SFrançois Tigeot 	const char *name;
39aee94f86SFrançois Tigeot 	i915_reg_t reg;
40a2fdbec6SFrançois Tigeot };
41a2fdbec6SFrançois Tigeot 
4219c468b4SFrançois Tigeot /* Map gmbus pin pairs to names and registers. */
4319c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins[] = {
4419c468b4SFrançois Tigeot 	[GMBUS_PIN_SSC] = { "ssc", GPIOB },
4519c468b4SFrançois Tigeot 	[GMBUS_PIN_VGADDC] = { "vga", GPIOA },
4619c468b4SFrançois Tigeot 	[GMBUS_PIN_PANEL] = { "panel", GPIOC },
4719c468b4SFrançois Tigeot 	[GMBUS_PIN_DPC] = { "dpc", GPIOD },
4819c468b4SFrançois Tigeot 	[GMBUS_PIN_DPB] = { "dpb", GPIOE },
4919c468b4SFrançois Tigeot 	[GMBUS_PIN_DPD] = { "dpd", GPIOF },
50a2fdbec6SFrançois Tigeot };
51bad0eccaSFrançois Tigeot 
5219c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins_bdw[] = {
5319c468b4SFrançois Tigeot 	[GMBUS_PIN_VGADDC] = { "vga", GPIOA },
5419c468b4SFrançois Tigeot 	[GMBUS_PIN_DPC] = { "dpc", GPIOD },
5519c468b4SFrançois Tigeot 	[GMBUS_PIN_DPB] = { "dpb", GPIOE },
5619c468b4SFrançois Tigeot 	[GMBUS_PIN_DPD] = { "dpd", GPIOF },
5719c468b4SFrançois Tigeot };
5819c468b4SFrançois Tigeot 
5919c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins_skl[] = {
6019c468b4SFrançois Tigeot 	[GMBUS_PIN_DPC] = { "dpc", GPIOD },
6119c468b4SFrançois Tigeot 	[GMBUS_PIN_DPB] = { "dpb", GPIOE },
6219c468b4SFrançois Tigeot 	[GMBUS_PIN_DPD] = { "dpd", GPIOF },
6319c468b4SFrançois Tigeot };
6419c468b4SFrançois Tigeot 
6519c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins_bxt[] = {
66aee94f86SFrançois Tigeot 	[GMBUS_PIN_1_BXT] = { "dpb", GPIOB },
67aee94f86SFrançois Tigeot 	[GMBUS_PIN_2_BXT] = { "dpc", GPIOC },
68aee94f86SFrançois Tigeot 	[GMBUS_PIN_3_BXT] = { "misc", GPIOD },
6919c468b4SFrançois Tigeot };
7019c468b4SFrançois Tigeot 
71*3f2dd94aSFrançois Tigeot static const struct gmbus_pin gmbus_pins_cnp[] = {
72*3f2dd94aSFrançois Tigeot 	[GMBUS_PIN_1_BXT] = { "dpb", GPIOB },
73*3f2dd94aSFrançois Tigeot 	[GMBUS_PIN_2_BXT] = { "dpc", GPIOC },
74*3f2dd94aSFrançois Tigeot 	[GMBUS_PIN_3_BXT] = { "misc", GPIOD },
75*3f2dd94aSFrançois Tigeot 	[GMBUS_PIN_4_CNP] = { "dpd", GPIOE },
76*3f2dd94aSFrançois Tigeot };
77*3f2dd94aSFrançois Tigeot 
7819c468b4SFrançois Tigeot /* pin is expected to be valid */
get_gmbus_pin(struct drm_i915_private * dev_priv,unsigned int pin)7919c468b4SFrançois Tigeot static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
8019c468b4SFrançois Tigeot 					     unsigned int pin)
8119c468b4SFrançois Tigeot {
82*3f2dd94aSFrançois Tigeot 	if (HAS_PCH_CNP(dev_priv))
83*3f2dd94aSFrançois Tigeot 		return &gmbus_pins_cnp[pin];
84*3f2dd94aSFrançois Tigeot 	else if (IS_GEN9_LP(dev_priv))
8519c468b4SFrançois Tigeot 		return &gmbus_pins_bxt[pin];
86a85cb24fSFrançois Tigeot 	else if (IS_GEN9_BC(dev_priv))
8719c468b4SFrançois Tigeot 		return &gmbus_pins_skl[pin];
8819c468b4SFrançois Tigeot 	else if (IS_BROADWELL(dev_priv))
8919c468b4SFrançois Tigeot 		return &gmbus_pins_bdw[pin];
9019c468b4SFrançois Tigeot 	else
9119c468b4SFrançois Tigeot 		return &gmbus_pins[pin];
9219c468b4SFrançois Tigeot }
9319c468b4SFrançois Tigeot 
intel_gmbus_is_valid_pin(struct drm_i915_private * dev_priv,unsigned int pin)9419c468b4SFrançois Tigeot bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
9519c468b4SFrançois Tigeot 			      unsigned int pin)
9619c468b4SFrançois Tigeot {
9719c468b4SFrançois Tigeot 	unsigned int size;
9819c468b4SFrançois Tigeot 
99*3f2dd94aSFrançois Tigeot 	if (HAS_PCH_CNP(dev_priv))
100*3f2dd94aSFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins_cnp);
101*3f2dd94aSFrançois Tigeot 	else if (IS_GEN9_LP(dev_priv))
10219c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins_bxt);
103a85cb24fSFrançois Tigeot 	else if (IS_GEN9_BC(dev_priv))
10419c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins_skl);
10519c468b4SFrançois Tigeot 	else if (IS_BROADWELL(dev_priv))
10619c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins_bdw);
10719c468b4SFrançois Tigeot 	else
10819c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins);
10919c468b4SFrançois Tigeot 
110aee94f86SFrançois Tigeot 	return pin < size &&
111aee94f86SFrançois Tigeot 		i915_mmio_reg_valid(get_gmbus_pin(dev_priv, pin)->reg);
11219c468b4SFrançois Tigeot }
11319c468b4SFrançois Tigeot 
114bad0eccaSFrançois Tigeot /* Intel GPIO access functions */
115bad0eccaSFrançois Tigeot 
116bad0eccaSFrançois Tigeot #define I2C_RISEFALL_TIME 10
117bad0eccaSFrançois Tigeot 
1189f4ca867SFrançois Tigeot static inline struct intel_gmbus *
to_intel_gmbus(struct i2c_adapter * i2c)1199f4ca867SFrançois Tigeot to_intel_gmbus(struct i2c_adapter *i2c)
1209f4ca867SFrançois Tigeot {
1219f4ca867SFrançois Tigeot 	return container_of(i2c, struct intel_gmbus, adapter);
1229f4ca867SFrançois Tigeot }
1239f4ca867SFrançois Tigeot 
124a2fdbec6SFrançois Tigeot void
intel_i2c_reset(struct drm_i915_private * dev_priv)125a85cb24fSFrançois Tigeot intel_i2c_reset(struct drm_i915_private *dev_priv)
126a2fdbec6SFrançois Tigeot {
127352ff8bdSFrançois Tigeot 	I915_WRITE(GMBUS0, 0);
128352ff8bdSFrançois Tigeot 	I915_WRITE(GMBUS4, 0);
129a2fdbec6SFrançois Tigeot }
130a2fdbec6SFrançois Tigeot 
intel_i2c_quirk_set(struct drm_i915_private * dev_priv,bool enable)131a2fdbec6SFrançois Tigeot static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
132bad0eccaSFrançois Tigeot {
133bad0eccaSFrançois Tigeot 	u32 val;
134bad0eccaSFrançois Tigeot 
135bad0eccaSFrançois Tigeot 	/* When using bit bashing for I2C, this bit needs to be set to 1 */
1368621f407SFrançois Tigeot 	if (!IS_PINEVIEW(dev_priv))
137bad0eccaSFrançois Tigeot 		return;
138bad0eccaSFrançois Tigeot 
139bad0eccaSFrançois Tigeot 	val = I915_READ(DSPCLK_GATE_D);
140bad0eccaSFrançois Tigeot 	if (enable)
141bad0eccaSFrançois Tigeot 		val |= DPCUNIT_CLOCK_GATE_DISABLE;
142bad0eccaSFrançois Tigeot 	else
143bad0eccaSFrançois Tigeot 		val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
144bad0eccaSFrançois Tigeot 	I915_WRITE(DSPCLK_GATE_D, val);
145bad0eccaSFrançois Tigeot }
146bad0eccaSFrançois Tigeot 
get_reserved(struct intel_gmbus * bus)1479f4ca867SFrançois Tigeot static u32 get_reserved(struct intel_gmbus *bus)
148a2fdbec6SFrançois Tigeot {
1499f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
150a2fdbec6SFrançois Tigeot 	u32 reserved = 0;
151a2fdbec6SFrançois Tigeot 
152a2fdbec6SFrançois Tigeot 	/* On most chips, these bits must be preserved in software. */
153a85cb24fSFrançois Tigeot 	if (!IS_I830(dev_priv) && !IS_I845G(dev_priv))
1549f4ca867SFrançois Tigeot 		reserved = I915_READ_NOTRACE(bus->gpio_reg) &
155a2fdbec6SFrançois Tigeot 					     (GPIO_DATA_PULLUP_DISABLE |
156a2fdbec6SFrançois Tigeot 					      GPIO_CLOCK_PULLUP_DISABLE);
157a2fdbec6SFrançois Tigeot 
158a2fdbec6SFrançois Tigeot 	return reserved;
159a2fdbec6SFrançois Tigeot }
160a2fdbec6SFrançois Tigeot 
get_clock(void * data)1619f4ca867SFrançois Tigeot static int get_clock(void *data)
162bad0eccaSFrançois Tigeot {
1639f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = data;
1649f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
1659f4ca867SFrançois Tigeot 	u32 reserved = get_reserved(bus);
1669f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
1679f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
1689f4ca867SFrançois Tigeot 	return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
169bad0eccaSFrançois Tigeot }
170bad0eccaSFrançois Tigeot 
get_data(void * data)1719f4ca867SFrançois Tigeot static int get_data(void *data)
172bad0eccaSFrançois Tigeot {
1739f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = data;
1749f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
1759f4ca867SFrançois Tigeot 	u32 reserved = get_reserved(bus);
1769f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
1779f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
1789f4ca867SFrançois Tigeot 	return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
179bad0eccaSFrançois Tigeot }
180bad0eccaSFrançois Tigeot 
set_clock(void * data,int state_high)1819f4ca867SFrançois Tigeot static void set_clock(void *data, int state_high)
182bad0eccaSFrançois Tigeot {
1839f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = data;
1849f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
1859f4ca867SFrançois Tigeot 	u32 reserved = get_reserved(bus);
1869f4ca867SFrançois Tigeot 	u32 clock_bits;
187bad0eccaSFrançois Tigeot 
1889f4ca867SFrançois Tigeot 	if (state_high)
189bad0eccaSFrançois Tigeot 		clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
190bad0eccaSFrançois Tigeot 	else
191bad0eccaSFrançois Tigeot 		clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
192bad0eccaSFrançois Tigeot 			GPIO_CLOCK_VAL_MASK;
193bad0eccaSFrançois Tigeot 
1949f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
1959f4ca867SFrançois Tigeot 	POSTING_READ(bus->gpio_reg);
196bad0eccaSFrançois Tigeot }
197bad0eccaSFrançois Tigeot 
set_data(void * data,int state_high)1989f4ca867SFrançois Tigeot static void set_data(void *data, int state_high)
199bad0eccaSFrançois Tigeot {
2009f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = data;
2019f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
2029f4ca867SFrançois Tigeot 	u32 reserved = get_reserved(bus);
203a2fdbec6SFrançois Tigeot 	u32 data_bits;
204bad0eccaSFrançois Tigeot 
2059f4ca867SFrançois Tigeot 	if (state_high)
206a2fdbec6SFrançois Tigeot 		data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
207a2fdbec6SFrançois Tigeot 	else
208a2fdbec6SFrançois Tigeot 		data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
209a2fdbec6SFrançois Tigeot 			GPIO_DATA_VAL_MASK;
210bad0eccaSFrançois Tigeot 
2119f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
2129f4ca867SFrançois Tigeot 	POSTING_READ(bus->gpio_reg);
213bad0eccaSFrançois Tigeot }
214bad0eccaSFrançois Tigeot 
215bad0eccaSFrançois Tigeot static int
intel_gpio_pre_xfer(struct i2c_adapter * adapter)2169f4ca867SFrançois Tigeot intel_gpio_pre_xfer(struct i2c_adapter *adapter)
217bad0eccaSFrançois Tigeot {
2189f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = container_of(adapter,
2199f4ca867SFrançois Tigeot 					       struct intel_gmbus,
2209f4ca867SFrançois Tigeot 					       adapter);
2219f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
222a2fdbec6SFrançois Tigeot 
223a85cb24fSFrançois Tigeot 	intel_i2c_reset(dev_priv);
224a2fdbec6SFrançois Tigeot 	intel_i2c_quirk_set(dev_priv, true);
2259f4ca867SFrançois Tigeot 	set_data(bus, 1);
2269f4ca867SFrançois Tigeot 	set_clock(bus, 1);
2279f4ca867SFrançois Tigeot 	udelay(I2C_RISEFALL_TIME);
2289f4ca867SFrançois Tigeot 	return 0;
229a2fdbec6SFrançois Tigeot }
230a2fdbec6SFrançois Tigeot 
2319f4ca867SFrançois Tigeot static void
intel_gpio_post_xfer(struct i2c_adapter * adapter)2329f4ca867SFrançois Tigeot intel_gpio_post_xfer(struct i2c_adapter *adapter)
2339f4ca867SFrançois Tigeot {
2349f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = container_of(adapter,
2359f4ca867SFrançois Tigeot 					       struct intel_gmbus,
2369f4ca867SFrançois Tigeot 					       adapter);
2379f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
2389f4ca867SFrançois Tigeot 
2399f4ca867SFrançois Tigeot 	set_data(bus, 1);
2409f4ca867SFrançois Tigeot 	set_clock(bus, 1);
2419f4ca867SFrançois Tigeot 	intel_i2c_quirk_set(dev_priv, false);
2429f4ca867SFrançois Tigeot }
2439f4ca867SFrançois Tigeot 
2449f4ca867SFrançois Tigeot static void
intel_gpio_setup(struct intel_gmbus * bus,unsigned int pin)2459f4ca867SFrançois Tigeot intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
2469f4ca867SFrançois Tigeot {
2479f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
2489f4ca867SFrançois Tigeot 	struct i2c_algo_bit_data *algo;
2499f4ca867SFrançois Tigeot 
2509f4ca867SFrançois Tigeot 	algo = &bus->bit_algo;
2519f4ca867SFrançois Tigeot 
252aee94f86SFrançois Tigeot 	bus->gpio_reg = _MMIO(dev_priv->gpio_mmio_base +
253aee94f86SFrançois Tigeot 			      i915_mmio_reg_offset(get_gmbus_pin(dev_priv, pin)->reg));
2549f4ca867SFrançois Tigeot 	bus->adapter.algo_data = algo;
2559f4ca867SFrançois Tigeot 	algo->setsda = set_data;
2569f4ca867SFrançois Tigeot 	algo->setscl = set_clock;
2579f4ca867SFrançois Tigeot 	algo->getsda = get_data;
2589f4ca867SFrançois Tigeot 	algo->getscl = get_clock;
2599f4ca867SFrançois Tigeot 	algo->pre_xfer = intel_gpio_pre_xfer;
2609f4ca867SFrançois Tigeot 	algo->post_xfer = intel_gpio_post_xfer;
2619f4ca867SFrançois Tigeot 	algo->udelay = I2C_RISEFALL_TIME;
2629f4ca867SFrançois Tigeot 	algo->timeout = usecs_to_jiffies(2200);
2639f4ca867SFrançois Tigeot 	algo->data = bus;
264a2fdbec6SFrançois Tigeot }
265a2fdbec6SFrançois Tigeot 
gmbus_wait(struct drm_i915_private * dev_priv,u32 status,u32 irq_en)2661e12ee3bSFrançois Tigeot static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en)
267a2fdbec6SFrançois Tigeot {
268a2fdbec6SFrançois Tigeot 	DEFINE_WAIT(wait);
2691e12ee3bSFrançois Tigeot 	u32 gmbus2;
2701e12ee3bSFrançois Tigeot 	int ret;
271a2fdbec6SFrançois Tigeot 
272a2fdbec6SFrançois Tigeot 	/* Important: The hw handles only the first bit, so set only one! Since
273a2fdbec6SFrançois Tigeot 	 * we also need to check for NAKs besides the hw ready/idle signal, we
2741e12ee3bSFrançois Tigeot 	 * need to wake up periodically and check that ourselves.
2751e12ee3bSFrançois Tigeot 	 */
2761e12ee3bSFrançois Tigeot 	if (!HAS_GMBUS_IRQ(dev_priv))
2771e12ee3bSFrançois Tigeot 		irq_en = 0;
278a2fdbec6SFrançois Tigeot 
2791e12ee3bSFrançois Tigeot 	add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
2801e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS4, irq_en);
281a2fdbec6SFrançois Tigeot 
2821e12ee3bSFrançois Tigeot 	status |= GMBUS_SATOER;
2831e12ee3bSFrançois Tigeot 	ret = wait_for_us((gmbus2 = I915_READ_FW(GMBUS2)) & status, 2);
2841e12ee3bSFrançois Tigeot 	if (ret)
2851e12ee3bSFrançois Tigeot 		ret = wait_for((gmbus2 = I915_READ_FW(GMBUS2)) & status, 50);
286a2fdbec6SFrançois Tigeot 
2871e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS4, 0);
2881e12ee3bSFrançois Tigeot 	remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
289a2fdbec6SFrançois Tigeot 
290a2fdbec6SFrançois Tigeot 	if (gmbus2 & GMBUS_SATOER)
291a2fdbec6SFrançois Tigeot 		return -ENXIO;
2921e12ee3bSFrançois Tigeot 
2931e12ee3bSFrançois Tigeot 	return ret;
294a2fdbec6SFrançois Tigeot }
295a2fdbec6SFrançois Tigeot 
296a2fdbec6SFrançois Tigeot static int
gmbus_wait_idle(struct drm_i915_private * dev_priv)297a2fdbec6SFrançois Tigeot gmbus_wait_idle(struct drm_i915_private *dev_priv)
298a2fdbec6SFrançois Tigeot {
2991e12ee3bSFrançois Tigeot 	DEFINE_WAIT(wait);
3001e12ee3bSFrançois Tigeot 	u32 irq_enable;
301a2fdbec6SFrançois Tigeot 	int ret;
302a2fdbec6SFrançois Tigeot 
3031e12ee3bSFrançois Tigeot 	/* Important: The hw handles only the first bit, so set only one! */
3041e12ee3bSFrançois Tigeot 	irq_enable = 0;
3051e12ee3bSFrançois Tigeot 	if (HAS_GMBUS_IRQ(dev_priv))
3061e12ee3bSFrançois Tigeot 		irq_enable = GMBUS_IDLE_EN;
3071e12ee3bSFrançois Tigeot 
3081e12ee3bSFrançois Tigeot 	add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
3091e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS4, irq_enable);
3101e12ee3bSFrançois Tigeot 
3111e12ee3bSFrançois Tigeot 	ret = intel_wait_for_register_fw(dev_priv,
3121487f786SFrançois Tigeot 					 GMBUS2, GMBUS_ACTIVE, 0,
3131487f786SFrançois Tigeot 					 10);
314a2fdbec6SFrançois Tigeot 
3151e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS4, 0);
3161e12ee3bSFrançois Tigeot 	remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
317a2fdbec6SFrançois Tigeot 
3181e12ee3bSFrançois Tigeot 	return ret;
319a2fdbec6SFrançois Tigeot }
320a2fdbec6SFrançois Tigeot 
321a2fdbec6SFrançois Tigeot static int
gmbus_xfer_read_chunk(struct drm_i915_private * dev_priv,unsigned short addr,u8 * buf,unsigned int len,u32 gmbus1_index)322477eb7f9SFrançois Tigeot gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
323477eb7f9SFrançois Tigeot 		      unsigned short addr, u8 *buf, unsigned int len,
324a2fdbec6SFrançois Tigeot 		      u32 gmbus1_index)
325a2fdbec6SFrançois Tigeot {
3261e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS1,
327a2fdbec6SFrançois Tigeot 		      gmbus1_index |
328a2fdbec6SFrançois Tigeot 		      GMBUS_CYCLE_WAIT |
329a2fdbec6SFrançois Tigeot 		      (len << GMBUS_BYTE_COUNT_SHIFT) |
330477eb7f9SFrançois Tigeot 		      (addr << GMBUS_SLAVE_ADDR_SHIFT) |
331a2fdbec6SFrançois Tigeot 		      GMBUS_SLAVE_READ | GMBUS_SW_RDY);
332a2fdbec6SFrançois Tigeot 	while (len) {
333a2fdbec6SFrançois Tigeot 		int ret;
334a2fdbec6SFrançois Tigeot 		u32 val, loop = 0;
335a2fdbec6SFrançois Tigeot 
3361e12ee3bSFrançois Tigeot 		ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
337a2fdbec6SFrançois Tigeot 		if (ret)
338a2fdbec6SFrançois Tigeot 			return ret;
339a2fdbec6SFrançois Tigeot 
3401e12ee3bSFrançois Tigeot 		val = I915_READ_FW(GMBUS3);
341a2fdbec6SFrançois Tigeot 		do {
342a2fdbec6SFrançois Tigeot 			*buf++ = val & 0xff;
343a2fdbec6SFrançois Tigeot 			val >>= 8;
344a2fdbec6SFrançois Tigeot 		} while (--len && ++loop < 4);
345a2fdbec6SFrançois Tigeot 	}
346a2fdbec6SFrançois Tigeot 
347a2fdbec6SFrançois Tigeot 	return 0;
348a2fdbec6SFrançois Tigeot }
349a2fdbec6SFrançois Tigeot 
350a2fdbec6SFrançois Tigeot static int
gmbus_xfer_read(struct drm_i915_private * dev_priv,struct i2c_msg * msg,u32 gmbus1_index)351477eb7f9SFrançois Tigeot gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
352477eb7f9SFrançois Tigeot 		u32 gmbus1_index)
353477eb7f9SFrançois Tigeot {
354477eb7f9SFrançois Tigeot 	u8 *buf = msg->buf;
355477eb7f9SFrançois Tigeot 	unsigned int rx_size = msg->len;
356477eb7f9SFrançois Tigeot 	unsigned int len;
357477eb7f9SFrançois Tigeot 	int ret;
358477eb7f9SFrançois Tigeot 
359477eb7f9SFrançois Tigeot 	do {
360477eb7f9SFrançois Tigeot 		len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
361477eb7f9SFrançois Tigeot 
3629f4ca867SFrançois Tigeot 		ret = gmbus_xfer_read_chunk(dev_priv, msg->addr,
363477eb7f9SFrançois Tigeot 					    buf, len, gmbus1_index);
364477eb7f9SFrançois Tigeot 		if (ret)
365477eb7f9SFrançois Tigeot 			return ret;
366477eb7f9SFrançois Tigeot 
367477eb7f9SFrançois Tigeot 		rx_size -= len;
368477eb7f9SFrançois Tigeot 		buf += len;
369477eb7f9SFrançois Tigeot 	} while (rx_size != 0);
370477eb7f9SFrançois Tigeot 
371477eb7f9SFrançois Tigeot 	return 0;
372477eb7f9SFrançois Tigeot }
373477eb7f9SFrançois Tigeot 
374477eb7f9SFrançois Tigeot static int
gmbus_xfer_write_chunk(struct drm_i915_private * dev_priv,unsigned short addr,u8 * buf,unsigned int len)375477eb7f9SFrançois Tigeot gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
376477eb7f9SFrançois Tigeot 		       unsigned short addr, u8 *buf, unsigned int len)
377a2fdbec6SFrançois Tigeot {
378477eb7f9SFrançois Tigeot 	unsigned int chunk_size = len;
379bad0eccaSFrançois Tigeot 	u32 val, loop;
380bad0eccaSFrançois Tigeot 
381a2fdbec6SFrançois Tigeot 	val = loop = 0;
382a2fdbec6SFrançois Tigeot 	while (len && loop < 4) {
383a2fdbec6SFrançois Tigeot 		val |= *buf++ << (8 * loop++);
384a2fdbec6SFrançois Tigeot 		len -= 1;
385a2fdbec6SFrançois Tigeot 	}
386a2fdbec6SFrançois Tigeot 
3871e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS3, val);
3881e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS1,
389a2fdbec6SFrançois Tigeot 		      GMBUS_CYCLE_WAIT |
390477eb7f9SFrançois Tigeot 		      (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
391477eb7f9SFrançois Tigeot 		      (addr << GMBUS_SLAVE_ADDR_SHIFT) |
392a2fdbec6SFrançois Tigeot 		      GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
393a2fdbec6SFrançois Tigeot 	while (len) {
394a2fdbec6SFrançois Tigeot 		int ret;
395a2fdbec6SFrançois Tigeot 
396a2fdbec6SFrançois Tigeot 		val = loop = 0;
397a2fdbec6SFrançois Tigeot 		do {
398a2fdbec6SFrançois Tigeot 			val |= *buf++ << (8 * loop);
399a2fdbec6SFrançois Tigeot 		} while (--len && ++loop < 4);
400a2fdbec6SFrançois Tigeot 
4011e12ee3bSFrançois Tigeot 		I915_WRITE_FW(GMBUS3, val);
402a2fdbec6SFrançois Tigeot 
4031e12ee3bSFrançois Tigeot 		ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
404a2fdbec6SFrançois Tigeot 		if (ret)
405a2fdbec6SFrançois Tigeot 			return ret;
406a2fdbec6SFrançois Tigeot 	}
407477eb7f9SFrançois Tigeot 
408477eb7f9SFrançois Tigeot 	return 0;
409477eb7f9SFrançois Tigeot }
410477eb7f9SFrançois Tigeot 
411477eb7f9SFrançois Tigeot static int
gmbus_xfer_write(struct drm_i915_private * dev_priv,struct i2c_msg * msg)412477eb7f9SFrançois Tigeot gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
413477eb7f9SFrançois Tigeot {
414477eb7f9SFrançois Tigeot 	u8 *buf = msg->buf;
415477eb7f9SFrançois Tigeot 	unsigned int tx_size = msg->len;
416477eb7f9SFrançois Tigeot 	unsigned int len;
417477eb7f9SFrançois Tigeot 	int ret;
418477eb7f9SFrançois Tigeot 
419477eb7f9SFrançois Tigeot 	do {
420477eb7f9SFrançois Tigeot 		len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
421477eb7f9SFrançois Tigeot 
4229f4ca867SFrançois Tigeot 		ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len);
423477eb7f9SFrançois Tigeot 		if (ret)
424477eb7f9SFrançois Tigeot 			return ret;
425477eb7f9SFrançois Tigeot 
426477eb7f9SFrançois Tigeot 		buf += len;
427477eb7f9SFrançois Tigeot 		tx_size -= len;
428477eb7f9SFrançois Tigeot 	} while (tx_size != 0);
429477eb7f9SFrançois Tigeot 
430a2fdbec6SFrançois Tigeot 	return 0;
431a2fdbec6SFrançois Tigeot }
432a2fdbec6SFrançois Tigeot 
433a2fdbec6SFrançois Tigeot /*
434a2fdbec6SFrançois Tigeot  * The gmbus controller can combine a 1 or 2 byte write with a read that
435a2fdbec6SFrançois Tigeot  * immediately follows it by using an "INDEX" cycle.
436a2fdbec6SFrançois Tigeot  */
437a2fdbec6SFrançois Tigeot static bool
gmbus_is_index_read(struct i2c_msg * msgs,int i,int num)438a2fdbec6SFrançois Tigeot gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
439a2fdbec6SFrançois Tigeot {
440a2fdbec6SFrançois Tigeot 	return (i + 1 < num &&
441*3f2dd94aSFrançois Tigeot 		msgs[i].addr == msgs[i + 1].addr &&
442*3f2dd94aSFrançois Tigeot 		!(msgs[i].flags & I2C_M_RD) &&
443*3f2dd94aSFrançois Tigeot 		(msgs[i].len == 1 || msgs[i].len == 2) &&
444a2fdbec6SFrançois Tigeot 		(msgs[i + 1].flags & I2C_M_RD));
445a2fdbec6SFrançois Tigeot }
446a2fdbec6SFrançois Tigeot 
447a2fdbec6SFrançois Tigeot static int
gmbus_xfer_index_read(struct drm_i915_private * dev_priv,struct i2c_msg * msgs)448a2fdbec6SFrançois Tigeot gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
449a2fdbec6SFrançois Tigeot {
450a2fdbec6SFrançois Tigeot 	u32 gmbus1_index = 0;
451a2fdbec6SFrançois Tigeot 	u32 gmbus5 = 0;
452a2fdbec6SFrançois Tigeot 	int ret;
453a2fdbec6SFrançois Tigeot 
454a2fdbec6SFrançois Tigeot 	if (msgs[0].len == 2)
455a2fdbec6SFrançois Tigeot 		gmbus5 = GMBUS_2BYTE_INDEX_EN |
456a2fdbec6SFrançois Tigeot 			 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
457a2fdbec6SFrançois Tigeot 	if (msgs[0].len == 1)
458a2fdbec6SFrançois Tigeot 		gmbus1_index = GMBUS_CYCLE_INDEX |
459a2fdbec6SFrançois Tigeot 			       (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
460a2fdbec6SFrançois Tigeot 
461a2fdbec6SFrançois Tigeot 	/* GMBUS5 holds 16-bit index */
462a2fdbec6SFrançois Tigeot 	if (gmbus5)
4631e12ee3bSFrançois Tigeot 		I915_WRITE_FW(GMBUS5, gmbus5);
464a2fdbec6SFrançois Tigeot 
465a2fdbec6SFrançois Tigeot 	ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
466a2fdbec6SFrançois Tigeot 
467a2fdbec6SFrançois Tigeot 	/* Clear GMBUS5 after each index transfer */
468a2fdbec6SFrançois Tigeot 	if (gmbus5)
4691e12ee3bSFrançois Tigeot 		I915_WRITE_FW(GMBUS5, 0);
470a2fdbec6SFrançois Tigeot 
471a2fdbec6SFrançois Tigeot 	return ret;
472a2fdbec6SFrançois Tigeot }
473a2fdbec6SFrançois Tigeot 
474a2fdbec6SFrançois Tigeot static int
do_gmbus_xfer(struct i2c_adapter * adapter,struct i2c_msg * msgs,int num)475aee94f86SFrançois Tigeot do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
476a2fdbec6SFrançois Tigeot {
4779f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = container_of(adapter,
4789f4ca867SFrançois Tigeot 					       struct intel_gmbus,
4799f4ca867SFrançois Tigeot 					       adapter);
4809f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
481352ff8bdSFrançois Tigeot 	int i = 0, inc, try = 0;
482a2fdbec6SFrançois Tigeot 	int ret = 0;
483a2fdbec6SFrançois Tigeot 
484477eb7f9SFrançois Tigeot retry:
4851e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS0, bus->reg0);
486bad0eccaSFrançois Tigeot 
487477eb7f9SFrançois Tigeot 	for (; i < num; i += inc) {
488477eb7f9SFrançois Tigeot 		inc = 1;
489a2fdbec6SFrançois Tigeot 		if (gmbus_is_index_read(msgs, i, num)) {
490a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
491477eb7f9SFrançois Tigeot 			inc = 2; /* an index read is two msgs */
492a2fdbec6SFrançois Tigeot 		} else if (msgs[i].flags & I2C_M_RD) {
493a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
494bad0eccaSFrançois Tigeot 		} else {
495a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_write(dev_priv, &msgs[i]);
496a2fdbec6SFrançois Tigeot 		}
497bad0eccaSFrançois Tigeot 
498aee94f86SFrançois Tigeot 		if (!ret)
4991e12ee3bSFrançois Tigeot 			ret = gmbus_wait(dev_priv,
5001e12ee3bSFrançois Tigeot 					 GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN);
501aee94f86SFrançois Tigeot 		if (ret == -ETIMEDOUT)
502a2fdbec6SFrançois Tigeot 			goto timeout;
503aee94f86SFrançois Tigeot 		else if (ret)
504aee94f86SFrançois Tigeot 			goto clear_err;
505bad0eccaSFrançois Tigeot 	}
506bad0eccaSFrançois Tigeot 
507a2fdbec6SFrançois Tigeot 	/* Generate a STOP condition on the bus. Note that gmbus can't generata
508a2fdbec6SFrançois Tigeot 	 * a STOP on the very first cycle. To simplify the code we
509a2fdbec6SFrançois Tigeot 	 * unconditionally generate the STOP condition with an additional gmbus
510a2fdbec6SFrançois Tigeot 	 * cycle. */
5111e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
512a2fdbec6SFrançois Tigeot 
513bad0eccaSFrançois Tigeot 	/* Mark the GMBUS interface as disabled after waiting for idle.
514bad0eccaSFrançois Tigeot 	 * We will re-enable it at the start of the next xfer,
515bad0eccaSFrançois Tigeot 	 * till then let it sleep.
516bad0eccaSFrançois Tigeot 	 */
517a2fdbec6SFrançois Tigeot 	if (gmbus_wait_idle(dev_priv)) {
518a2fdbec6SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
5199f4ca867SFrançois Tigeot 			 adapter->name);
520a2fdbec6SFrançois Tigeot 		ret = -ETIMEDOUT;
521a2fdbec6SFrançois Tigeot 	}
5221e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS0, 0);
523a2fdbec6SFrançois Tigeot 	ret = ret ?: i;
5249f4ca867SFrançois Tigeot 	goto out;
525bad0eccaSFrançois Tigeot 
526bad0eccaSFrançois Tigeot clear_err:
527a2fdbec6SFrançois Tigeot 	/*
528a2fdbec6SFrançois Tigeot 	 * Wait for bus to IDLE before clearing NAK.
529a2fdbec6SFrançois Tigeot 	 * If we clear the NAK while bus is still active, then it will stay
530a2fdbec6SFrançois Tigeot 	 * active and the next transaction may fail.
531a2fdbec6SFrançois Tigeot 	 *
532a2fdbec6SFrançois Tigeot 	 * If no ACK is received during the address phase of a transaction, the
533a2fdbec6SFrançois Tigeot 	 * adapter must report -ENXIO. It is not clear what to return if no ACK
534a2fdbec6SFrançois Tigeot 	 * is received at other times. But we have to be careful to not return
535a2fdbec6SFrançois Tigeot 	 * spurious -ENXIO because that will prevent i2c and drm edid functions
536a2fdbec6SFrançois Tigeot 	 * from retrying. So return -ENXIO only when gmbus properly quiescents -
537a2fdbec6SFrançois Tigeot 	 * timing out seems to happen when there _is_ a ddc chip present, but
538a2fdbec6SFrançois Tigeot 	 * it's slow responding and only answers on the 2nd retry.
539a2fdbec6SFrançois Tigeot 	 */
540a2fdbec6SFrançois Tigeot 	ret = -ENXIO;
541a2fdbec6SFrançois Tigeot 	if (gmbus_wait_idle(dev_priv)) {
542a2fdbec6SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
5439f4ca867SFrançois Tigeot 			      adapter->name);
544a2fdbec6SFrançois Tigeot 		ret = -ETIMEDOUT;
545a2fdbec6SFrançois Tigeot 	}
546a2fdbec6SFrançois Tigeot 
547bad0eccaSFrançois Tigeot 	/* Toggle the Software Clear Interrupt bit. This has the effect
548bad0eccaSFrançois Tigeot 	 * of resetting the GMBUS controller and so clearing the
549bad0eccaSFrançois Tigeot 	 * BUS_ERROR raised by the slave's NAK.
550bad0eccaSFrançois Tigeot 	 */
5511e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS1, GMBUS_SW_CLR_INT);
5521e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS1, 0);
5531e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS0, 0);
554bad0eccaSFrançois Tigeot 
555a2fdbec6SFrançois Tigeot 	DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
5569f4ca867SFrançois Tigeot 			 adapter->name, msgs[i].addr,
557a2fdbec6SFrançois Tigeot 			 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
558bad0eccaSFrançois Tigeot 
559477eb7f9SFrançois Tigeot 	/*
560477eb7f9SFrançois Tigeot 	 * Passive adapters sometimes NAK the first probe. Retry the first
561477eb7f9SFrançois Tigeot 	 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
562477eb7f9SFrançois Tigeot 	 * has retries internally. See also the retry loop in
563477eb7f9SFrançois Tigeot 	 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
564477eb7f9SFrançois Tigeot 	 */
565477eb7f9SFrançois Tigeot 	if (ret == -ENXIO && i == 0 && try++ == 0) {
566477eb7f9SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
5679f4ca867SFrançois Tigeot 			      adapter->name);
568477eb7f9SFrançois Tigeot 		goto retry;
569477eb7f9SFrançois Tigeot 	}
570477eb7f9SFrançois Tigeot 
571bad0eccaSFrançois Tigeot 	goto out;
572a2fdbec6SFrançois Tigeot 
573a2fdbec6SFrançois Tigeot timeout:
5748621f407SFrançois Tigeot 	DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
5759f4ca867SFrançois Tigeot 		      bus->adapter.name, bus->reg0 & 0xff);
5761e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS0, 0);
577a2fdbec6SFrançois Tigeot 
578aee94f86SFrançois Tigeot 	/*
579aee94f86SFrançois Tigeot 	 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
580aee94f86SFrançois Tigeot 	 * instead. Use EAGAIN to have i2c core retry.
581aee94f86SFrançois Tigeot 	 */
582aee94f86SFrançois Tigeot 	ret = -EAGAIN;
583a2fdbec6SFrançois Tigeot 
584a2fdbec6SFrançois Tigeot out:
585aee94f86SFrançois Tigeot 	return ret;
586aee94f86SFrançois Tigeot }
587352ff8bdSFrançois Tigeot 
588aee94f86SFrançois Tigeot static int
gmbus_xfer(struct i2c_adapter * adapter,struct i2c_msg * msgs,int num)589aee94f86SFrançois Tigeot gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
590aee94f86SFrançois Tigeot {
591aee94f86SFrançois Tigeot 	struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus,
592aee94f86SFrançois Tigeot 					       adapter);
593aee94f86SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
594aee94f86SFrançois Tigeot 	int ret;
595aee94f86SFrançois Tigeot 
596aee94f86SFrançois Tigeot 	intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
597aee94f86SFrançois Tigeot 
5988621f407SFrançois Tigeot 	if (bus->force_bit) {
599aee94f86SFrançois Tigeot 		ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
6008621f407SFrançois Tigeot 		if (ret < 0)
6018621f407SFrançois Tigeot 			bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY;
6028621f407SFrançois Tigeot 	} else {
603aee94f86SFrançois Tigeot 		ret = do_gmbus_xfer(adapter, msgs, num);
6048621f407SFrançois Tigeot 		if (ret == -EAGAIN)
6058621f407SFrançois Tigeot 			bus->force_bit |= GMBUS_FORCE_BIT_RETRY;
6068621f407SFrançois Tigeot 	}
607aee94f86SFrançois Tigeot 
608352ff8bdSFrançois Tigeot 	intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
609352ff8bdSFrançois Tigeot 
610a2fdbec6SFrançois Tigeot 	return ret;
611bad0eccaSFrançois Tigeot }
612bad0eccaSFrançois Tigeot 
gmbus_func(struct i2c_adapter * adapter)6139f4ca867SFrançois Tigeot static u32 gmbus_func(struct i2c_adapter *adapter)
61419df918dSFrançois Tigeot {
6159f4ca867SFrançois Tigeot 	return i2c_bit_algo.functionality(adapter) &
6169f4ca867SFrançois Tigeot 		(I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
6179f4ca867SFrançois Tigeot 		/* I2C_FUNC_10BIT_ADDR | */
6189f4ca867SFrançois Tigeot 		I2C_FUNC_SMBUS_READ_BLOCK_DATA |
6199f4ca867SFrançois Tigeot 		I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
62019df918dSFrançois Tigeot }
62119df918dSFrançois Tigeot 
6229f4ca867SFrançois Tigeot static const struct i2c_algorithm gmbus_algorithm = {
6239f4ca867SFrançois Tigeot 	.master_xfer	= gmbus_xfer,
6249f4ca867SFrançois Tigeot 	.functionality	= gmbus_func
625bad0eccaSFrançois Tigeot };
626a2fdbec6SFrançois Tigeot 
gmbus_lock_bus(struct i2c_adapter * adapter,unsigned int flags)627*3f2dd94aSFrançois Tigeot static void gmbus_lock_bus(struct i2c_adapter *adapter,
628*3f2dd94aSFrançois Tigeot 			   unsigned int flags)
629*3f2dd94aSFrançois Tigeot {
630*3f2dd94aSFrançois Tigeot 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
631*3f2dd94aSFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
632*3f2dd94aSFrançois Tigeot 
633*3f2dd94aSFrançois Tigeot 	mutex_lock(&dev_priv->gmbus_mutex);
634*3f2dd94aSFrançois Tigeot }
635*3f2dd94aSFrançois Tigeot 
gmbus_trylock_bus(struct i2c_adapter * adapter,unsigned int flags)636*3f2dd94aSFrançois Tigeot static int gmbus_trylock_bus(struct i2c_adapter *adapter,
637*3f2dd94aSFrançois Tigeot 			     unsigned int flags)
638*3f2dd94aSFrançois Tigeot {
639*3f2dd94aSFrançois Tigeot 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
640*3f2dd94aSFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
641*3f2dd94aSFrançois Tigeot 
642*3f2dd94aSFrançois Tigeot 	return mutex_trylock(&dev_priv->gmbus_mutex);
643*3f2dd94aSFrançois Tigeot }
644*3f2dd94aSFrançois Tigeot 
gmbus_unlock_bus(struct i2c_adapter * adapter,unsigned int flags)645*3f2dd94aSFrançois Tigeot static void gmbus_unlock_bus(struct i2c_adapter *adapter,
646*3f2dd94aSFrançois Tigeot 			     unsigned int flags)
647*3f2dd94aSFrançois Tigeot {
648*3f2dd94aSFrançois Tigeot 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
649*3f2dd94aSFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
650*3f2dd94aSFrançois Tigeot 
651*3f2dd94aSFrançois Tigeot 	mutex_unlock(&dev_priv->gmbus_mutex);
652*3f2dd94aSFrançois Tigeot }
653*3f2dd94aSFrançois Tigeot 
654*3f2dd94aSFrançois Tigeot static const struct i2c_lock_operations gmbus_lock_ops = {
655*3f2dd94aSFrançois Tigeot 	.lock_bus =    gmbus_lock_bus,
656*3f2dd94aSFrançois Tigeot 	.trylock_bus = gmbus_trylock_bus,
657*3f2dd94aSFrançois Tigeot 	.unlock_bus =  gmbus_unlock_bus,
658*3f2dd94aSFrançois Tigeot };
659*3f2dd94aSFrançois Tigeot 
66019c468b4SFrançois Tigeot /**
66119c468b4SFrançois Tigeot  * intel_gmbus_setup - instantiate all Intel i2c GMBuses
662a85cb24fSFrançois Tigeot  * @dev_priv: i915 device private
66319c468b4SFrançois Tigeot  */
intel_setup_gmbus(struct drm_i915_private * dev_priv)664a85cb24fSFrançois Tigeot int intel_setup_gmbus(struct drm_i915_private *dev_priv)
665bad0eccaSFrançois Tigeot {
6661e12ee3bSFrançois Tigeot 	struct pci_dev *pdev = dev_priv->drm.pdev;
6679f4ca867SFrançois Tigeot 	struct intel_gmbus *bus;
66819c468b4SFrançois Tigeot 	unsigned int pin;
66919c468b4SFrançois Tigeot 	int ret;
670bad0eccaSFrançois Tigeot 
6711e12ee3bSFrançois Tigeot 	if (HAS_PCH_NOP(dev_priv))
6728e26cdf6SFrançois Tigeot 		return 0;
673aee94f86SFrançois Tigeot 
6741e12ee3bSFrançois Tigeot 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
675a2fdbec6SFrançois Tigeot 		dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
676aee94f86SFrançois Tigeot 	else if (!HAS_GMCH_DISPLAY(dev_priv))
677aee94f86SFrançois Tigeot 		dev_priv->gpio_mmio_base =
678aee94f86SFrançois Tigeot 			i915_mmio_reg_offset(PCH_GPIOA) -
679aee94f86SFrançois Tigeot 			i915_mmio_reg_offset(GPIOA);
680a2fdbec6SFrançois Tigeot 
68119df918dSFrançois Tigeot 	lockinit(&dev_priv->gmbus_mutex, "gmbus", 0, LK_CANRECURSE);
682a2fdbec6SFrançois Tigeot 	init_waitqueue_head(&dev_priv->gmbus_wait_queue);
683a2fdbec6SFrançois Tigeot 
6849f4ca867SFrançois Tigeot 	for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
68519c468b4SFrançois Tigeot 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
68619c468b4SFrançois Tigeot 			continue;
68719c468b4SFrançois Tigeot 
6889f4ca867SFrançois Tigeot 		bus = &dev_priv->gmbus[pin];
6899f4ca867SFrançois Tigeot 
6909f4ca867SFrançois Tigeot #if 0
6919f4ca867SFrançois Tigeot 		bus->adapter.owner = THIS_MODULE;
6929f4ca867SFrançois Tigeot 		bus->adapter.class = I2C_CLASS_DDC;
6939f4ca867SFrançois Tigeot #endif
6949f4ca867SFrançois Tigeot 		ksnprintf(bus->adapter.name,
6959f4ca867SFrançois Tigeot 			 sizeof(bus->adapter.name),
6969f4ca867SFrançois Tigeot 			 "i915 gmbus %s",
6979f4ca867SFrançois Tigeot 			 get_gmbus_pin(dev_priv, pin)->name);
6989f4ca867SFrançois Tigeot 
6991e12ee3bSFrançois Tigeot 		bus->adapter.dev.parent = &pdev->dev;
7009f4ca867SFrançois Tigeot 		bus->dev_priv = dev_priv;
7019f4ca867SFrançois Tigeot 
7029f4ca867SFrançois Tigeot 		bus->adapter.algo = &gmbus_algorithm;
703*3f2dd94aSFrançois Tigeot 		bus->adapter.lock_ops = &gmbus_lock_ops;
7049f4ca867SFrançois Tigeot 
705aee94f86SFrançois Tigeot 		/*
706aee94f86SFrançois Tigeot 		 * We wish to retry with bit banging
707aee94f86SFrançois Tigeot 		 * after a timed out GMBUS attempt.
708aee94f86SFrançois Tigeot 		 */
709aee94f86SFrançois Tigeot 		bus->adapter.retries = 1;
710aee94f86SFrançois Tigeot 
7119f4ca867SFrançois Tigeot 		/* By default use a conservative clock rate */
7129f4ca867SFrançois Tigeot 		bus->reg0 = pin | GMBUS_RATE_100KHZ;
7139f4ca867SFrançois Tigeot 
7149f4ca867SFrançois Tigeot 		/* gmbus seems to be broken on i830 */
7151e12ee3bSFrançois Tigeot 		if (IS_I830(dev_priv))
7169f4ca867SFrançois Tigeot 			bus->force_bit = 1;
7179f4ca867SFrançois Tigeot 
7189f4ca867SFrançois Tigeot 		intel_gpio_setup(bus, pin);
7199f4ca867SFrançois Tigeot 
7209f4ca867SFrançois Tigeot 		ret = i2c_add_adapter(&bus->adapter);
7219f4ca867SFrançois Tigeot 		if (ret)
722bad0eccaSFrançois Tigeot 			goto err;
723bad0eccaSFrançois Tigeot 	}
724bad0eccaSFrançois Tigeot 
725a85cb24fSFrançois Tigeot 	intel_i2c_reset(dev_priv);
726bad0eccaSFrançois Tigeot 
7279f4ca867SFrançois Tigeot 	return 0;
728bad0eccaSFrançois Tigeot 
729bad0eccaSFrançois Tigeot err:
730aee94f86SFrançois Tigeot 	while (pin--) {
7319f4ca867SFrançois Tigeot 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
7329f4ca867SFrançois Tigeot 			continue;
7339f4ca867SFrançois Tigeot 
7349f4ca867SFrançois Tigeot 		bus = &dev_priv->gmbus[pin];
7359f4ca867SFrançois Tigeot 		i2c_del_adapter(&bus->adapter);
7369f4ca867SFrançois Tigeot 	}
7379f4ca867SFrançois Tigeot 	return ret;
738bad0eccaSFrançois Tigeot }
739bad0eccaSFrançois Tigeot 
intel_gmbus_get_adapter(struct drm_i915_private * dev_priv,unsigned int pin)7409f4ca867SFrançois Tigeot struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
7419f4ca867SFrançois Tigeot 					    unsigned int pin)
742bad0eccaSFrançois Tigeot {
7439f4ca867SFrançois Tigeot 	if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
7449f4ca867SFrançois Tigeot 		return NULL;
745bad0eccaSFrançois Tigeot 
7469f4ca867SFrançois Tigeot 	return &dev_priv->gmbus[pin].adapter;
747bad0eccaSFrançois Tigeot }
748bad0eccaSFrançois Tigeot 
intel_gmbus_set_speed(struct i2c_adapter * adapter,int speed)7499f4ca867SFrançois Tigeot void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
750bad0eccaSFrançois Tigeot {
7519f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
752bad0eccaSFrançois Tigeot 
7539f4ca867SFrançois Tigeot 	bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
7549f4ca867SFrançois Tigeot }
7559f4ca867SFrançois Tigeot 
intel_gmbus_force_bit(struct i2c_adapter * adapter,bool force_bit)7569f4ca867SFrançois Tigeot void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
7579f4ca867SFrançois Tigeot {
7589f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
7598621f407SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
7608621f407SFrançois Tigeot 
7618621f407SFrançois Tigeot 	mutex_lock(&dev_priv->gmbus_mutex);
7629f4ca867SFrançois Tigeot 
7639f4ca867SFrançois Tigeot 	bus->force_bit += force_bit ? 1 : -1;
7649f4ca867SFrançois Tigeot 	DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
7659f4ca867SFrançois Tigeot 		      force_bit ? "en" : "dis", adapter->name,
7669f4ca867SFrançois Tigeot 		      bus->force_bit);
7678621f407SFrançois Tigeot 
7688621f407SFrançois Tigeot 	mutex_unlock(&dev_priv->gmbus_mutex);
7699f4ca867SFrançois Tigeot }
7709f4ca867SFrançois Tigeot 
intel_teardown_gmbus(struct drm_i915_private * dev_priv)771a85cb24fSFrançois Tigeot void intel_teardown_gmbus(struct drm_i915_private *dev_priv)
7729f4ca867SFrançois Tigeot {
7739f4ca867SFrançois Tigeot 	struct intel_gmbus *bus;
7749f4ca867SFrançois Tigeot 	unsigned int pin;
7759f4ca867SFrançois Tigeot 
7769f4ca867SFrançois Tigeot 	for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
7779f4ca867SFrançois Tigeot 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
7789f4ca867SFrançois Tigeot 			continue;
7799f4ca867SFrançois Tigeot 
7809f4ca867SFrançois Tigeot 		bus = &dev_priv->gmbus[pin];
7819f4ca867SFrançois Tigeot 		i2c_del_adapter(&bus->adapter);
7829f4ca867SFrançois Tigeot 	}
783bad0eccaSFrançois Tigeot }
784