xref: /dflybsd-src/sys/dev/drm/i915/intel_i2c.c (revision a85cb24f18e3804e75ab8bcda7692564d0563317)
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 
7119c468b4SFrançois Tigeot /* pin is expected to be valid */
7219c468b4SFrançois Tigeot static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
7319c468b4SFrançois Tigeot 					     unsigned int pin)
7419c468b4SFrançois Tigeot {
75*a85cb24fSFrançois Tigeot 	if (IS_GEN9_LP(dev_priv))
7619c468b4SFrançois Tigeot 		return &gmbus_pins_bxt[pin];
77*a85cb24fSFrançois Tigeot 	else if (IS_GEN9_BC(dev_priv))
7819c468b4SFrançois Tigeot 		return &gmbus_pins_skl[pin];
7919c468b4SFrançois Tigeot 	else if (IS_BROADWELL(dev_priv))
8019c468b4SFrançois Tigeot 		return &gmbus_pins_bdw[pin];
8119c468b4SFrançois Tigeot 	else
8219c468b4SFrançois Tigeot 		return &gmbus_pins[pin];
8319c468b4SFrançois Tigeot }
8419c468b4SFrançois Tigeot 
8519c468b4SFrançois Tigeot bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
8619c468b4SFrançois Tigeot 			      unsigned int pin)
8719c468b4SFrançois Tigeot {
8819c468b4SFrançois Tigeot 	unsigned int size;
8919c468b4SFrançois Tigeot 
90*a85cb24fSFrançois Tigeot 	if (IS_GEN9_LP(dev_priv))
9119c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins_bxt);
92*a85cb24fSFrançois Tigeot 	else if (IS_GEN9_BC(dev_priv))
9319c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins_skl);
9419c468b4SFrançois Tigeot 	else if (IS_BROADWELL(dev_priv))
9519c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins_bdw);
9619c468b4SFrançois Tigeot 	else
9719c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins);
9819c468b4SFrançois Tigeot 
99aee94f86SFrançois Tigeot 	return pin < size &&
100aee94f86SFrançois Tigeot 		i915_mmio_reg_valid(get_gmbus_pin(dev_priv, pin)->reg);
10119c468b4SFrançois Tigeot }
10219c468b4SFrançois Tigeot 
103bad0eccaSFrançois Tigeot /* Intel GPIO access functions */
104bad0eccaSFrançois Tigeot 
105bad0eccaSFrançois Tigeot #define I2C_RISEFALL_TIME 10
106bad0eccaSFrançois Tigeot 
1079f4ca867SFrançois Tigeot static inline struct intel_gmbus *
1089f4ca867SFrançois Tigeot to_intel_gmbus(struct i2c_adapter *i2c)
1099f4ca867SFrançois Tigeot {
1109f4ca867SFrançois Tigeot 	return container_of(i2c, struct intel_gmbus, adapter);
1119f4ca867SFrançois Tigeot }
1129f4ca867SFrançois Tigeot 
113a2fdbec6SFrançois Tigeot void
114*a85cb24fSFrançois Tigeot intel_i2c_reset(struct drm_i915_private *dev_priv)
115a2fdbec6SFrançois Tigeot {
116352ff8bdSFrançois Tigeot 	I915_WRITE(GMBUS0, 0);
117352ff8bdSFrançois Tigeot 	I915_WRITE(GMBUS4, 0);
118a2fdbec6SFrançois Tigeot }
119a2fdbec6SFrançois Tigeot 
120a2fdbec6SFrançois Tigeot static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
121bad0eccaSFrançois Tigeot {
122bad0eccaSFrançois Tigeot 	u32 val;
123bad0eccaSFrançois Tigeot 
124bad0eccaSFrançois Tigeot 	/* When using bit bashing for I2C, this bit needs to be set to 1 */
1258621f407SFrançois Tigeot 	if (!IS_PINEVIEW(dev_priv))
126bad0eccaSFrançois Tigeot 		return;
127bad0eccaSFrançois Tigeot 
128bad0eccaSFrançois Tigeot 	val = I915_READ(DSPCLK_GATE_D);
129bad0eccaSFrançois Tigeot 	if (enable)
130bad0eccaSFrançois Tigeot 		val |= DPCUNIT_CLOCK_GATE_DISABLE;
131bad0eccaSFrançois Tigeot 	else
132bad0eccaSFrançois Tigeot 		val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
133bad0eccaSFrançois Tigeot 	I915_WRITE(DSPCLK_GATE_D, val);
134bad0eccaSFrançois Tigeot }
135bad0eccaSFrançois Tigeot 
1369f4ca867SFrançois Tigeot static u32 get_reserved(struct intel_gmbus *bus)
137a2fdbec6SFrançois Tigeot {
1389f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
139a2fdbec6SFrançois Tigeot 	u32 reserved = 0;
140a2fdbec6SFrançois Tigeot 
141a2fdbec6SFrançois Tigeot 	/* On most chips, these bits must be preserved in software. */
142*a85cb24fSFrançois Tigeot 	if (!IS_I830(dev_priv) && !IS_I845G(dev_priv))
1439f4ca867SFrançois Tigeot 		reserved = I915_READ_NOTRACE(bus->gpio_reg) &
144a2fdbec6SFrançois Tigeot 					     (GPIO_DATA_PULLUP_DISABLE |
145a2fdbec6SFrançois Tigeot 					      GPIO_CLOCK_PULLUP_DISABLE);
146a2fdbec6SFrançois Tigeot 
147a2fdbec6SFrançois Tigeot 	return reserved;
148a2fdbec6SFrançois Tigeot }
149a2fdbec6SFrançois Tigeot 
1509f4ca867SFrançois Tigeot static int get_clock(void *data)
151bad0eccaSFrançois Tigeot {
1529f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = data;
1539f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
1549f4ca867SFrançois Tigeot 	u32 reserved = get_reserved(bus);
1559f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
1569f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
1579f4ca867SFrançois Tigeot 	return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
158bad0eccaSFrançois Tigeot }
159bad0eccaSFrançois Tigeot 
1609f4ca867SFrançois Tigeot static int get_data(void *data)
161bad0eccaSFrançois Tigeot {
1629f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = data;
1639f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
1649f4ca867SFrançois Tigeot 	u32 reserved = get_reserved(bus);
1659f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
1669f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
1679f4ca867SFrançois Tigeot 	return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
168bad0eccaSFrançois Tigeot }
169bad0eccaSFrançois Tigeot 
1709f4ca867SFrançois Tigeot static void set_clock(void *data, int state_high)
171bad0eccaSFrançois Tigeot {
1729f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = data;
1739f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
1749f4ca867SFrançois Tigeot 	u32 reserved = get_reserved(bus);
1759f4ca867SFrançois Tigeot 	u32 clock_bits;
176bad0eccaSFrançois Tigeot 
1779f4ca867SFrançois Tigeot 	if (state_high)
178bad0eccaSFrançois Tigeot 		clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
179bad0eccaSFrançois Tigeot 	else
180bad0eccaSFrançois Tigeot 		clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
181bad0eccaSFrançois Tigeot 			GPIO_CLOCK_VAL_MASK;
182bad0eccaSFrançois Tigeot 
1839f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
1849f4ca867SFrançois Tigeot 	POSTING_READ(bus->gpio_reg);
185bad0eccaSFrançois Tigeot }
186bad0eccaSFrançois Tigeot 
1879f4ca867SFrançois Tigeot static void set_data(void *data, int state_high)
188bad0eccaSFrançois Tigeot {
1899f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = data;
1909f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
1919f4ca867SFrançois Tigeot 	u32 reserved = get_reserved(bus);
192a2fdbec6SFrançois Tigeot 	u32 data_bits;
193bad0eccaSFrançois Tigeot 
1949f4ca867SFrançois Tigeot 	if (state_high)
195a2fdbec6SFrançois Tigeot 		data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
196a2fdbec6SFrançois Tigeot 	else
197a2fdbec6SFrançois Tigeot 		data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
198a2fdbec6SFrançois Tigeot 			GPIO_DATA_VAL_MASK;
199bad0eccaSFrançois Tigeot 
2009f4ca867SFrançois Tigeot 	I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
2019f4ca867SFrançois Tigeot 	POSTING_READ(bus->gpio_reg);
202bad0eccaSFrançois Tigeot }
203bad0eccaSFrançois Tigeot 
204bad0eccaSFrançois Tigeot static int
2059f4ca867SFrançois Tigeot intel_gpio_pre_xfer(struct i2c_adapter *adapter)
206bad0eccaSFrançois Tigeot {
2079f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = container_of(adapter,
2089f4ca867SFrançois Tigeot 					       struct intel_gmbus,
2099f4ca867SFrançois Tigeot 					       adapter);
2109f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
211a2fdbec6SFrançois Tigeot 
212*a85cb24fSFrançois Tigeot 	intel_i2c_reset(dev_priv);
213a2fdbec6SFrançois Tigeot 	intel_i2c_quirk_set(dev_priv, true);
2149f4ca867SFrançois Tigeot 	set_data(bus, 1);
2159f4ca867SFrançois Tigeot 	set_clock(bus, 1);
2169f4ca867SFrançois Tigeot 	udelay(I2C_RISEFALL_TIME);
2179f4ca867SFrançois Tigeot 	return 0;
218a2fdbec6SFrançois Tigeot }
219a2fdbec6SFrançois Tigeot 
2209f4ca867SFrançois Tigeot static void
2219f4ca867SFrançois Tigeot intel_gpio_post_xfer(struct i2c_adapter *adapter)
2229f4ca867SFrançois Tigeot {
2239f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = container_of(adapter,
2249f4ca867SFrançois Tigeot 					       struct intel_gmbus,
2259f4ca867SFrançois Tigeot 					       adapter);
2269f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
2279f4ca867SFrançois Tigeot 
2289f4ca867SFrançois Tigeot 	set_data(bus, 1);
2299f4ca867SFrançois Tigeot 	set_clock(bus, 1);
2309f4ca867SFrançois Tigeot 	intel_i2c_quirk_set(dev_priv, false);
2319f4ca867SFrançois Tigeot }
2329f4ca867SFrançois Tigeot 
2339f4ca867SFrançois Tigeot static void
2349f4ca867SFrançois Tigeot intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
2359f4ca867SFrançois Tigeot {
2369f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
2379f4ca867SFrançois Tigeot 	struct i2c_algo_bit_data *algo;
2389f4ca867SFrançois Tigeot 
2399f4ca867SFrançois Tigeot 	algo = &bus->bit_algo;
2409f4ca867SFrançois Tigeot 
241aee94f86SFrançois Tigeot 	bus->gpio_reg = _MMIO(dev_priv->gpio_mmio_base +
242aee94f86SFrançois Tigeot 			      i915_mmio_reg_offset(get_gmbus_pin(dev_priv, pin)->reg));
2439f4ca867SFrançois Tigeot 	bus->adapter.algo_data = algo;
2449f4ca867SFrançois Tigeot 	algo->setsda = set_data;
2459f4ca867SFrançois Tigeot 	algo->setscl = set_clock;
2469f4ca867SFrançois Tigeot 	algo->getsda = get_data;
2479f4ca867SFrançois Tigeot 	algo->getscl = get_clock;
2489f4ca867SFrançois Tigeot 	algo->pre_xfer = intel_gpio_pre_xfer;
2499f4ca867SFrançois Tigeot 	algo->post_xfer = intel_gpio_post_xfer;
2509f4ca867SFrançois Tigeot 	algo->udelay = I2C_RISEFALL_TIME;
2519f4ca867SFrançois Tigeot 	algo->timeout = usecs_to_jiffies(2200);
2529f4ca867SFrançois Tigeot 	algo->data = bus;
253a2fdbec6SFrançois Tigeot }
254a2fdbec6SFrançois Tigeot 
2551e12ee3bSFrançois Tigeot static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en)
256a2fdbec6SFrançois Tigeot {
257a2fdbec6SFrançois Tigeot 	DEFINE_WAIT(wait);
2581e12ee3bSFrançois Tigeot 	u32 gmbus2;
2591e12ee3bSFrançois Tigeot 	int ret;
260a2fdbec6SFrançois Tigeot 
261a2fdbec6SFrançois Tigeot 	/* Important: The hw handles only the first bit, so set only one! Since
262a2fdbec6SFrançois Tigeot 	 * we also need to check for NAKs besides the hw ready/idle signal, we
2631e12ee3bSFrançois Tigeot 	 * need to wake up periodically and check that ourselves.
2641e12ee3bSFrançois Tigeot 	 */
2651e12ee3bSFrançois Tigeot 	if (!HAS_GMBUS_IRQ(dev_priv))
2661e12ee3bSFrançois Tigeot 		irq_en = 0;
267a2fdbec6SFrançois Tigeot 
2681e12ee3bSFrançois Tigeot 	add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
2691e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS4, irq_en);
270a2fdbec6SFrançois Tigeot 
2711e12ee3bSFrançois Tigeot 	status |= GMBUS_SATOER;
2721e12ee3bSFrançois Tigeot 	ret = wait_for_us((gmbus2 = I915_READ_FW(GMBUS2)) & status, 2);
2731e12ee3bSFrançois Tigeot 	if (ret)
2741e12ee3bSFrançois Tigeot 		ret = wait_for((gmbus2 = I915_READ_FW(GMBUS2)) & status, 50);
275a2fdbec6SFrançois Tigeot 
2761e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS4, 0);
2771e12ee3bSFrançois Tigeot 	remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
278a2fdbec6SFrançois Tigeot 
279a2fdbec6SFrançois Tigeot 	if (gmbus2 & GMBUS_SATOER)
280a2fdbec6SFrançois Tigeot 		return -ENXIO;
2811e12ee3bSFrançois Tigeot 
2821e12ee3bSFrançois Tigeot 	return ret;
283a2fdbec6SFrançois Tigeot }
284a2fdbec6SFrançois Tigeot 
285a2fdbec6SFrançois Tigeot static int
286a2fdbec6SFrançois Tigeot gmbus_wait_idle(struct drm_i915_private *dev_priv)
287a2fdbec6SFrançois Tigeot {
2881e12ee3bSFrançois Tigeot 	DEFINE_WAIT(wait);
2891e12ee3bSFrançois Tigeot 	u32 irq_enable;
290a2fdbec6SFrançois Tigeot 	int ret;
291a2fdbec6SFrançois Tigeot 
2921e12ee3bSFrançois Tigeot 	/* Important: The hw handles only the first bit, so set only one! */
2931e12ee3bSFrançois Tigeot 	irq_enable = 0;
2941e12ee3bSFrançois Tigeot 	if (HAS_GMBUS_IRQ(dev_priv))
2951e12ee3bSFrançois Tigeot 		irq_enable = GMBUS_IDLE_EN;
2961e12ee3bSFrançois Tigeot 
2971e12ee3bSFrançois Tigeot 	add_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
2981e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS4, irq_enable);
2991e12ee3bSFrançois Tigeot 
3001e12ee3bSFrançois Tigeot 	ret = intel_wait_for_register_fw(dev_priv,
3011487f786SFrançois Tigeot 					 GMBUS2, GMBUS_ACTIVE, 0,
3021487f786SFrançois Tigeot 					 10);
303a2fdbec6SFrançois Tigeot 
3041e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS4, 0);
3051e12ee3bSFrançois Tigeot 	remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait);
306a2fdbec6SFrançois Tigeot 
3071e12ee3bSFrançois Tigeot 	return ret;
308a2fdbec6SFrançois Tigeot }
309a2fdbec6SFrançois Tigeot 
310a2fdbec6SFrançois Tigeot static int
311477eb7f9SFrançois Tigeot gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
312477eb7f9SFrançois Tigeot 		      unsigned short addr, u8 *buf, unsigned int len,
313a2fdbec6SFrançois Tigeot 		      u32 gmbus1_index)
314a2fdbec6SFrançois Tigeot {
3151e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS1,
316a2fdbec6SFrançois Tigeot 		      gmbus1_index |
317a2fdbec6SFrançois Tigeot 		      GMBUS_CYCLE_WAIT |
318a2fdbec6SFrançois Tigeot 		      (len << GMBUS_BYTE_COUNT_SHIFT) |
319477eb7f9SFrançois Tigeot 		      (addr << GMBUS_SLAVE_ADDR_SHIFT) |
320a2fdbec6SFrançois Tigeot 		      GMBUS_SLAVE_READ | GMBUS_SW_RDY);
321a2fdbec6SFrançois Tigeot 	while (len) {
322a2fdbec6SFrançois Tigeot 		int ret;
323a2fdbec6SFrançois Tigeot 		u32 val, loop = 0;
324a2fdbec6SFrançois Tigeot 
3251e12ee3bSFrançois Tigeot 		ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
326a2fdbec6SFrançois Tigeot 		if (ret)
327a2fdbec6SFrançois Tigeot 			return ret;
328a2fdbec6SFrançois Tigeot 
3291e12ee3bSFrançois Tigeot 		val = I915_READ_FW(GMBUS3);
330a2fdbec6SFrançois Tigeot 		do {
331a2fdbec6SFrançois Tigeot 			*buf++ = val & 0xff;
332a2fdbec6SFrançois Tigeot 			val >>= 8;
333a2fdbec6SFrançois Tigeot 		} while (--len && ++loop < 4);
334a2fdbec6SFrançois Tigeot 	}
335a2fdbec6SFrançois Tigeot 
336a2fdbec6SFrançois Tigeot 	return 0;
337a2fdbec6SFrançois Tigeot }
338a2fdbec6SFrançois Tigeot 
339a2fdbec6SFrançois Tigeot static int
340477eb7f9SFrançois Tigeot gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
341477eb7f9SFrançois Tigeot 		u32 gmbus1_index)
342477eb7f9SFrançois Tigeot {
343477eb7f9SFrançois Tigeot 	u8 *buf = msg->buf;
344477eb7f9SFrançois Tigeot 	unsigned int rx_size = msg->len;
345477eb7f9SFrançois Tigeot 	unsigned int len;
346477eb7f9SFrançois Tigeot 	int ret;
347477eb7f9SFrançois Tigeot 
348477eb7f9SFrançois Tigeot 	do {
349477eb7f9SFrançois Tigeot 		len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
350477eb7f9SFrançois Tigeot 
3519f4ca867SFrançois Tigeot 		ret = gmbus_xfer_read_chunk(dev_priv, msg->addr,
352477eb7f9SFrançois Tigeot 					    buf, len, gmbus1_index);
353477eb7f9SFrançois Tigeot 		if (ret)
354477eb7f9SFrançois Tigeot 			return ret;
355477eb7f9SFrançois Tigeot 
356477eb7f9SFrançois Tigeot 		rx_size -= len;
357477eb7f9SFrançois Tigeot 		buf += len;
358477eb7f9SFrançois Tigeot 	} while (rx_size != 0);
359477eb7f9SFrançois Tigeot 
360477eb7f9SFrançois Tigeot 	return 0;
361477eb7f9SFrançois Tigeot }
362477eb7f9SFrançois Tigeot 
363477eb7f9SFrançois Tigeot static int
364477eb7f9SFrançois Tigeot gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
365477eb7f9SFrançois Tigeot 		       unsigned short addr, u8 *buf, unsigned int len)
366a2fdbec6SFrançois Tigeot {
367477eb7f9SFrançois Tigeot 	unsigned int chunk_size = len;
368bad0eccaSFrançois Tigeot 	u32 val, loop;
369bad0eccaSFrançois Tigeot 
370a2fdbec6SFrançois Tigeot 	val = loop = 0;
371a2fdbec6SFrançois Tigeot 	while (len && loop < 4) {
372a2fdbec6SFrançois Tigeot 		val |= *buf++ << (8 * loop++);
373a2fdbec6SFrançois Tigeot 		len -= 1;
374a2fdbec6SFrançois Tigeot 	}
375a2fdbec6SFrançois Tigeot 
3761e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS3, val);
3771e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS1,
378a2fdbec6SFrançois Tigeot 		      GMBUS_CYCLE_WAIT |
379477eb7f9SFrançois Tigeot 		      (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
380477eb7f9SFrançois Tigeot 		      (addr << GMBUS_SLAVE_ADDR_SHIFT) |
381a2fdbec6SFrançois Tigeot 		      GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
382a2fdbec6SFrançois Tigeot 	while (len) {
383a2fdbec6SFrançois Tigeot 		int ret;
384a2fdbec6SFrançois Tigeot 
385a2fdbec6SFrançois Tigeot 		val = loop = 0;
386a2fdbec6SFrançois Tigeot 		do {
387a2fdbec6SFrançois Tigeot 			val |= *buf++ << (8 * loop);
388a2fdbec6SFrançois Tigeot 		} while (--len && ++loop < 4);
389a2fdbec6SFrançois Tigeot 
3901e12ee3bSFrançois Tigeot 		I915_WRITE_FW(GMBUS3, val);
391a2fdbec6SFrançois Tigeot 
3921e12ee3bSFrançois Tigeot 		ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN);
393a2fdbec6SFrançois Tigeot 		if (ret)
394a2fdbec6SFrançois Tigeot 			return ret;
395a2fdbec6SFrançois Tigeot 	}
396477eb7f9SFrançois Tigeot 
397477eb7f9SFrançois Tigeot 	return 0;
398477eb7f9SFrançois Tigeot }
399477eb7f9SFrançois Tigeot 
400477eb7f9SFrançois Tigeot static int
401477eb7f9SFrançois Tigeot gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
402477eb7f9SFrançois Tigeot {
403477eb7f9SFrançois Tigeot 	u8 *buf = msg->buf;
404477eb7f9SFrançois Tigeot 	unsigned int tx_size = msg->len;
405477eb7f9SFrançois Tigeot 	unsigned int len;
406477eb7f9SFrançois Tigeot 	int ret;
407477eb7f9SFrançois Tigeot 
408477eb7f9SFrançois Tigeot 	do {
409477eb7f9SFrançois Tigeot 		len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
410477eb7f9SFrançois Tigeot 
4119f4ca867SFrançois Tigeot 		ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len);
412477eb7f9SFrançois Tigeot 		if (ret)
413477eb7f9SFrançois Tigeot 			return ret;
414477eb7f9SFrançois Tigeot 
415477eb7f9SFrançois Tigeot 		buf += len;
416477eb7f9SFrançois Tigeot 		tx_size -= len;
417477eb7f9SFrançois Tigeot 	} while (tx_size != 0);
418477eb7f9SFrançois Tigeot 
419a2fdbec6SFrançois Tigeot 	return 0;
420a2fdbec6SFrançois Tigeot }
421a2fdbec6SFrançois Tigeot 
422a2fdbec6SFrançois Tigeot /*
423a2fdbec6SFrançois Tigeot  * The gmbus controller can combine a 1 or 2 byte write with a read that
424a2fdbec6SFrançois Tigeot  * immediately follows it by using an "INDEX" cycle.
425a2fdbec6SFrançois Tigeot  */
426a2fdbec6SFrançois Tigeot static bool
427a2fdbec6SFrançois Tigeot gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
428a2fdbec6SFrançois Tigeot {
429a2fdbec6SFrançois Tigeot 	return (i + 1 < num &&
430a2fdbec6SFrançois Tigeot 		!(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
431a2fdbec6SFrançois Tigeot 		(msgs[i + 1].flags & I2C_M_RD));
432a2fdbec6SFrançois Tigeot }
433a2fdbec6SFrançois Tigeot 
434a2fdbec6SFrançois Tigeot static int
435a2fdbec6SFrançois Tigeot gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
436a2fdbec6SFrançois Tigeot {
437a2fdbec6SFrançois Tigeot 	u32 gmbus1_index = 0;
438a2fdbec6SFrançois Tigeot 	u32 gmbus5 = 0;
439a2fdbec6SFrançois Tigeot 	int ret;
440a2fdbec6SFrançois Tigeot 
441a2fdbec6SFrançois Tigeot 	if (msgs[0].len == 2)
442a2fdbec6SFrançois Tigeot 		gmbus5 = GMBUS_2BYTE_INDEX_EN |
443a2fdbec6SFrançois Tigeot 			 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
444a2fdbec6SFrançois Tigeot 	if (msgs[0].len == 1)
445a2fdbec6SFrançois Tigeot 		gmbus1_index = GMBUS_CYCLE_INDEX |
446a2fdbec6SFrançois Tigeot 			       (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
447a2fdbec6SFrançois Tigeot 
448a2fdbec6SFrançois Tigeot 	/* GMBUS5 holds 16-bit index */
449a2fdbec6SFrançois Tigeot 	if (gmbus5)
4501e12ee3bSFrançois Tigeot 		I915_WRITE_FW(GMBUS5, gmbus5);
451a2fdbec6SFrançois Tigeot 
452a2fdbec6SFrançois Tigeot 	ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
453a2fdbec6SFrançois Tigeot 
454a2fdbec6SFrançois Tigeot 	/* Clear GMBUS5 after each index transfer */
455a2fdbec6SFrançois Tigeot 	if (gmbus5)
4561e12ee3bSFrançois Tigeot 		I915_WRITE_FW(GMBUS5, 0);
457a2fdbec6SFrançois Tigeot 
458a2fdbec6SFrançois Tigeot 	return ret;
459a2fdbec6SFrançois Tigeot }
460a2fdbec6SFrançois Tigeot 
461a2fdbec6SFrançois Tigeot static int
462aee94f86SFrançois Tigeot do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
463a2fdbec6SFrançois Tigeot {
4649f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = container_of(adapter,
4659f4ca867SFrançois Tigeot 					       struct intel_gmbus,
4669f4ca867SFrançois Tigeot 					       adapter);
4679f4ca867SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
468352ff8bdSFrançois Tigeot 	int i = 0, inc, try = 0;
469a2fdbec6SFrançois Tigeot 	int ret = 0;
470a2fdbec6SFrançois Tigeot 
471477eb7f9SFrançois Tigeot retry:
4721e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS0, bus->reg0);
473bad0eccaSFrançois Tigeot 
474477eb7f9SFrançois Tigeot 	for (; i < num; i += inc) {
475477eb7f9SFrançois Tigeot 		inc = 1;
476a2fdbec6SFrançois Tigeot 		if (gmbus_is_index_read(msgs, i, num)) {
477a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
478477eb7f9SFrançois Tigeot 			inc = 2; /* an index read is two msgs */
479a2fdbec6SFrançois Tigeot 		} else if (msgs[i].flags & I2C_M_RD) {
480a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
481bad0eccaSFrançois Tigeot 		} else {
482a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_write(dev_priv, &msgs[i]);
483a2fdbec6SFrançois Tigeot 		}
484bad0eccaSFrançois Tigeot 
485aee94f86SFrançois Tigeot 		if (!ret)
4861e12ee3bSFrançois Tigeot 			ret = gmbus_wait(dev_priv,
4871e12ee3bSFrançois Tigeot 					 GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN);
488aee94f86SFrançois Tigeot 		if (ret == -ETIMEDOUT)
489a2fdbec6SFrançois Tigeot 			goto timeout;
490aee94f86SFrançois Tigeot 		else if (ret)
491aee94f86SFrançois Tigeot 			goto clear_err;
492bad0eccaSFrançois Tigeot 	}
493bad0eccaSFrançois Tigeot 
494a2fdbec6SFrançois Tigeot 	/* Generate a STOP condition on the bus. Note that gmbus can't generata
495a2fdbec6SFrançois Tigeot 	 * a STOP on the very first cycle. To simplify the code we
496a2fdbec6SFrançois Tigeot 	 * unconditionally generate the STOP condition with an additional gmbus
497a2fdbec6SFrançois Tigeot 	 * cycle. */
4981e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
499a2fdbec6SFrançois Tigeot 
500bad0eccaSFrançois Tigeot 	/* Mark the GMBUS interface as disabled after waiting for idle.
501bad0eccaSFrançois Tigeot 	 * We will re-enable it at the start of the next xfer,
502bad0eccaSFrançois Tigeot 	 * till then let it sleep.
503bad0eccaSFrançois Tigeot 	 */
504a2fdbec6SFrançois Tigeot 	if (gmbus_wait_idle(dev_priv)) {
505a2fdbec6SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
5069f4ca867SFrançois Tigeot 			 adapter->name);
507a2fdbec6SFrançois Tigeot 		ret = -ETIMEDOUT;
508a2fdbec6SFrançois Tigeot 	}
5091e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS0, 0);
510a2fdbec6SFrançois Tigeot 	ret = ret ?: i;
5119f4ca867SFrançois Tigeot 	goto out;
512bad0eccaSFrançois Tigeot 
513bad0eccaSFrançois Tigeot clear_err:
514a2fdbec6SFrançois Tigeot 	/*
515a2fdbec6SFrançois Tigeot 	 * Wait for bus to IDLE before clearing NAK.
516a2fdbec6SFrançois Tigeot 	 * If we clear the NAK while bus is still active, then it will stay
517a2fdbec6SFrançois Tigeot 	 * active and the next transaction may fail.
518a2fdbec6SFrançois Tigeot 	 *
519a2fdbec6SFrançois Tigeot 	 * If no ACK is received during the address phase of a transaction, the
520a2fdbec6SFrançois Tigeot 	 * adapter must report -ENXIO. It is not clear what to return if no ACK
521a2fdbec6SFrançois Tigeot 	 * is received at other times. But we have to be careful to not return
522a2fdbec6SFrançois Tigeot 	 * spurious -ENXIO because that will prevent i2c and drm edid functions
523a2fdbec6SFrançois Tigeot 	 * from retrying. So return -ENXIO only when gmbus properly quiescents -
524a2fdbec6SFrançois Tigeot 	 * timing out seems to happen when there _is_ a ddc chip present, but
525a2fdbec6SFrançois Tigeot 	 * it's slow responding and only answers on the 2nd retry.
526a2fdbec6SFrançois Tigeot 	 */
527a2fdbec6SFrançois Tigeot 	ret = -ENXIO;
528a2fdbec6SFrançois Tigeot 	if (gmbus_wait_idle(dev_priv)) {
529a2fdbec6SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
5309f4ca867SFrançois Tigeot 			      adapter->name);
531a2fdbec6SFrançois Tigeot 		ret = -ETIMEDOUT;
532a2fdbec6SFrançois Tigeot 	}
533a2fdbec6SFrançois Tigeot 
534bad0eccaSFrançois Tigeot 	/* Toggle the Software Clear Interrupt bit. This has the effect
535bad0eccaSFrançois Tigeot 	 * of resetting the GMBUS controller and so clearing the
536bad0eccaSFrançois Tigeot 	 * BUS_ERROR raised by the slave's NAK.
537bad0eccaSFrançois Tigeot 	 */
5381e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS1, GMBUS_SW_CLR_INT);
5391e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS1, 0);
5401e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS0, 0);
541bad0eccaSFrançois Tigeot 
542a2fdbec6SFrançois Tigeot 	DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
5439f4ca867SFrançois Tigeot 			 adapter->name, msgs[i].addr,
544a2fdbec6SFrançois Tigeot 			 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
545bad0eccaSFrançois Tigeot 
546477eb7f9SFrançois Tigeot 	/*
547477eb7f9SFrançois Tigeot 	 * Passive adapters sometimes NAK the first probe. Retry the first
548477eb7f9SFrançois Tigeot 	 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
549477eb7f9SFrançois Tigeot 	 * has retries internally. See also the retry loop in
550477eb7f9SFrançois Tigeot 	 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
551477eb7f9SFrançois Tigeot 	 */
552477eb7f9SFrançois Tigeot 	if (ret == -ENXIO && i == 0 && try++ == 0) {
553477eb7f9SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
5549f4ca867SFrançois Tigeot 			      adapter->name);
555477eb7f9SFrançois Tigeot 		goto retry;
556477eb7f9SFrançois Tigeot 	}
557477eb7f9SFrançois Tigeot 
558bad0eccaSFrançois Tigeot 	goto out;
559a2fdbec6SFrançois Tigeot 
560a2fdbec6SFrançois Tigeot timeout:
5618621f407SFrançois Tigeot 	DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
5629f4ca867SFrançois Tigeot 		      bus->adapter.name, bus->reg0 & 0xff);
5631e12ee3bSFrançois Tigeot 	I915_WRITE_FW(GMBUS0, 0);
564a2fdbec6SFrançois Tigeot 
565aee94f86SFrançois Tigeot 	/*
566aee94f86SFrançois Tigeot 	 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging
567aee94f86SFrançois Tigeot 	 * instead. Use EAGAIN to have i2c core retry.
568aee94f86SFrançois Tigeot 	 */
569aee94f86SFrançois Tigeot 	ret = -EAGAIN;
570a2fdbec6SFrançois Tigeot 
571a2fdbec6SFrançois Tigeot out:
572aee94f86SFrançois Tigeot 	return ret;
573aee94f86SFrançois Tigeot }
574352ff8bdSFrançois Tigeot 
575aee94f86SFrançois Tigeot static int
576aee94f86SFrançois Tigeot gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num)
577aee94f86SFrançois Tigeot {
578aee94f86SFrançois Tigeot 	struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus,
579aee94f86SFrançois Tigeot 					       adapter);
580aee94f86SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
581aee94f86SFrançois Tigeot 	int ret;
582aee94f86SFrançois Tigeot 
583aee94f86SFrançois Tigeot 	intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
584aee94f86SFrançois Tigeot 	mutex_lock(&dev_priv->gmbus_mutex);
585aee94f86SFrançois Tigeot 
5868621f407SFrançois Tigeot 	if (bus->force_bit) {
587aee94f86SFrançois Tigeot 		ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
5888621f407SFrançois Tigeot 		if (ret < 0)
5898621f407SFrançois Tigeot 			bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY;
5908621f407SFrançois Tigeot 	} else {
591aee94f86SFrançois Tigeot 		ret = do_gmbus_xfer(adapter, msgs, num);
5928621f407SFrançois Tigeot 		if (ret == -EAGAIN)
5938621f407SFrançois Tigeot 			bus->force_bit |= GMBUS_FORCE_BIT_RETRY;
5948621f407SFrançois Tigeot 	}
595aee94f86SFrançois Tigeot 
596aee94f86SFrançois Tigeot 	mutex_unlock(&dev_priv->gmbus_mutex);
597352ff8bdSFrançois Tigeot 	intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
598352ff8bdSFrançois Tigeot 
599a2fdbec6SFrançois Tigeot 	return ret;
600bad0eccaSFrançois Tigeot }
601bad0eccaSFrançois Tigeot 
6029f4ca867SFrançois Tigeot static u32 gmbus_func(struct i2c_adapter *adapter)
60319df918dSFrançois Tigeot {
6049f4ca867SFrançois Tigeot 	return i2c_bit_algo.functionality(adapter) &
6059f4ca867SFrançois Tigeot 		(I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
6069f4ca867SFrançois Tigeot 		/* I2C_FUNC_10BIT_ADDR | */
6079f4ca867SFrançois Tigeot 		I2C_FUNC_SMBUS_READ_BLOCK_DATA |
6089f4ca867SFrançois Tigeot 		I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
60919df918dSFrançois Tigeot }
61019df918dSFrançois Tigeot 
6119f4ca867SFrançois Tigeot static const struct i2c_algorithm gmbus_algorithm = {
6129f4ca867SFrançois Tigeot 	.master_xfer	= gmbus_xfer,
6139f4ca867SFrançois Tigeot 	.functionality	= gmbus_func
614bad0eccaSFrançois Tigeot };
615a2fdbec6SFrançois Tigeot 
61619c468b4SFrançois Tigeot /**
61719c468b4SFrançois Tigeot  * intel_gmbus_setup - instantiate all Intel i2c GMBuses
618*a85cb24fSFrançois Tigeot  * @dev_priv: i915 device private
61919c468b4SFrançois Tigeot  */
620*a85cb24fSFrançois Tigeot int intel_setup_gmbus(struct drm_i915_private *dev_priv)
621bad0eccaSFrançois Tigeot {
6221e12ee3bSFrançois Tigeot 	struct pci_dev *pdev = dev_priv->drm.pdev;
6239f4ca867SFrançois Tigeot 	struct intel_gmbus *bus;
62419c468b4SFrançois Tigeot 	unsigned int pin;
62519c468b4SFrançois Tigeot 	int ret;
626bad0eccaSFrançois Tigeot 
6271e12ee3bSFrançois Tigeot 	if (HAS_PCH_NOP(dev_priv))
6288e26cdf6SFrançois Tigeot 		return 0;
629aee94f86SFrançois Tigeot 
6301e12ee3bSFrançois Tigeot 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
631a2fdbec6SFrançois Tigeot 		dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
632aee94f86SFrançois Tigeot 	else if (!HAS_GMCH_DISPLAY(dev_priv))
633aee94f86SFrançois Tigeot 		dev_priv->gpio_mmio_base =
634aee94f86SFrançois Tigeot 			i915_mmio_reg_offset(PCH_GPIOA) -
635aee94f86SFrançois Tigeot 			i915_mmio_reg_offset(GPIOA);
636a2fdbec6SFrançois Tigeot 
63719df918dSFrançois Tigeot 	lockinit(&dev_priv->gmbus_mutex, "gmbus", 0, LK_CANRECURSE);
638a2fdbec6SFrançois Tigeot 	init_waitqueue_head(&dev_priv->gmbus_wait_queue);
639a2fdbec6SFrançois Tigeot 
6409f4ca867SFrançois Tigeot 	for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
64119c468b4SFrançois Tigeot 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
64219c468b4SFrançois Tigeot 			continue;
64319c468b4SFrançois Tigeot 
6449f4ca867SFrançois Tigeot 		bus = &dev_priv->gmbus[pin];
6459f4ca867SFrançois Tigeot 
6469f4ca867SFrançois Tigeot #if 0
6479f4ca867SFrançois Tigeot 		bus->adapter.owner = THIS_MODULE;
6489f4ca867SFrançois Tigeot 		bus->adapter.class = I2C_CLASS_DDC;
6499f4ca867SFrançois Tigeot #endif
6509f4ca867SFrançois Tigeot 		ksnprintf(bus->adapter.name,
6519f4ca867SFrançois Tigeot 			 sizeof(bus->adapter.name),
6529f4ca867SFrançois Tigeot 			 "i915 gmbus %s",
6539f4ca867SFrançois Tigeot 			 get_gmbus_pin(dev_priv, pin)->name);
6549f4ca867SFrançois Tigeot 
6551e12ee3bSFrançois Tigeot 		bus->adapter.dev.parent = &pdev->dev;
6569f4ca867SFrançois Tigeot 		bus->dev_priv = dev_priv;
6579f4ca867SFrançois Tigeot 
6589f4ca867SFrançois Tigeot 		bus->adapter.algo = &gmbus_algorithm;
6599f4ca867SFrançois Tigeot 
660aee94f86SFrançois Tigeot 		/*
661aee94f86SFrançois Tigeot 		 * We wish to retry with bit banging
662aee94f86SFrançois Tigeot 		 * after a timed out GMBUS attempt.
663aee94f86SFrançois Tigeot 		 */
664aee94f86SFrançois Tigeot 		bus->adapter.retries = 1;
665aee94f86SFrançois Tigeot 
6669f4ca867SFrançois Tigeot 		/* By default use a conservative clock rate */
6679f4ca867SFrançois Tigeot 		bus->reg0 = pin | GMBUS_RATE_100KHZ;
6689f4ca867SFrançois Tigeot 
6699f4ca867SFrançois Tigeot 		/* gmbus seems to be broken on i830 */
6701e12ee3bSFrançois Tigeot 		if (IS_I830(dev_priv))
6719f4ca867SFrançois Tigeot 			bus->force_bit = 1;
6729f4ca867SFrançois Tigeot 
6739f4ca867SFrançois Tigeot 		intel_gpio_setup(bus, pin);
6749f4ca867SFrançois Tigeot 
6759f4ca867SFrançois Tigeot 		ret = i2c_add_adapter(&bus->adapter);
6769f4ca867SFrançois Tigeot 		if (ret)
677bad0eccaSFrançois Tigeot 			goto err;
678bad0eccaSFrançois Tigeot 	}
679bad0eccaSFrançois Tigeot 
680*a85cb24fSFrançois Tigeot 	intel_i2c_reset(dev_priv);
681bad0eccaSFrançois Tigeot 
6829f4ca867SFrançois Tigeot 	return 0;
683bad0eccaSFrançois Tigeot 
684bad0eccaSFrançois Tigeot err:
685aee94f86SFrançois Tigeot 	while (pin--) {
6869f4ca867SFrançois Tigeot 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
6879f4ca867SFrançois Tigeot 			continue;
6889f4ca867SFrançois Tigeot 
6899f4ca867SFrançois Tigeot 		bus = &dev_priv->gmbus[pin];
6909f4ca867SFrançois Tigeot 		i2c_del_adapter(&bus->adapter);
6919f4ca867SFrançois Tigeot 	}
6929f4ca867SFrançois Tigeot 	return ret;
693bad0eccaSFrançois Tigeot }
694bad0eccaSFrançois Tigeot 
6959f4ca867SFrançois Tigeot struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
6969f4ca867SFrançois Tigeot 					    unsigned int pin)
697bad0eccaSFrançois Tigeot {
6989f4ca867SFrançois Tigeot 	if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
6999f4ca867SFrançois Tigeot 		return NULL;
700bad0eccaSFrançois Tigeot 
7019f4ca867SFrançois Tigeot 	return &dev_priv->gmbus[pin].adapter;
702bad0eccaSFrançois Tigeot }
703bad0eccaSFrançois Tigeot 
7049f4ca867SFrançois Tigeot void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
705bad0eccaSFrançois Tigeot {
7069f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
707bad0eccaSFrançois Tigeot 
7089f4ca867SFrançois Tigeot 	bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
7099f4ca867SFrançois Tigeot }
7109f4ca867SFrançois Tigeot 
7119f4ca867SFrançois Tigeot void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
7129f4ca867SFrançois Tigeot {
7139f4ca867SFrançois Tigeot 	struct intel_gmbus *bus = to_intel_gmbus(adapter);
7148621f407SFrançois Tigeot 	struct drm_i915_private *dev_priv = bus->dev_priv;
7158621f407SFrançois Tigeot 
7168621f407SFrançois Tigeot 	mutex_lock(&dev_priv->gmbus_mutex);
7179f4ca867SFrançois Tigeot 
7189f4ca867SFrançois Tigeot 	bus->force_bit += force_bit ? 1 : -1;
7199f4ca867SFrançois Tigeot 	DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
7209f4ca867SFrançois Tigeot 		      force_bit ? "en" : "dis", adapter->name,
7219f4ca867SFrançois Tigeot 		      bus->force_bit);
7228621f407SFrançois Tigeot 
7238621f407SFrançois Tigeot 	mutex_unlock(&dev_priv->gmbus_mutex);
7249f4ca867SFrançois Tigeot }
7259f4ca867SFrançois Tigeot 
726*a85cb24fSFrançois Tigeot void intel_teardown_gmbus(struct drm_i915_private *dev_priv)
7279f4ca867SFrançois Tigeot {
7289f4ca867SFrançois Tigeot 	struct intel_gmbus *bus;
7299f4ca867SFrançois Tigeot 	unsigned int pin;
7309f4ca867SFrançois Tigeot 
7319f4ca867SFrançois Tigeot 	for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
7329f4ca867SFrançois Tigeot 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
7339f4ca867SFrançois Tigeot 			continue;
7349f4ca867SFrançois Tigeot 
7359f4ca867SFrançois Tigeot 		bus = &dev_priv->gmbus[pin];
7369f4ca867SFrançois Tigeot 		i2c_del_adapter(&bus->adapter);
7379f4ca867SFrançois Tigeot 	}
738bad0eccaSFrançois Tigeot }
739