xref: /dflybsd-src/sys/dev/drm/i915/intel_i2c.c (revision 175896df11e05ada921c872a66c1ed71f15fba3a)
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  *
29bad0eccaSFrançois Tigeot  * Copyright (c) 2011 The FreeBSD Foundation
30bad0eccaSFrançois Tigeot  * All rights reserved.
31bad0eccaSFrançois Tigeot  *
32bad0eccaSFrançois Tigeot  * This software was developed by Konstantin Belousov under sponsorship from
33bad0eccaSFrançois Tigeot  * the FreeBSD Foundation.
34bad0eccaSFrançois Tigeot  *
35bad0eccaSFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
36bad0eccaSFrançois Tigeot  * modification, are permitted provided that the following conditions
37bad0eccaSFrançois Tigeot  * are met:
38bad0eccaSFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
39bad0eccaSFrançois Tigeot  *    notice, this list of conditions and the following disclaimer.
40bad0eccaSFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
41bad0eccaSFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
42bad0eccaSFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
43bad0eccaSFrançois Tigeot  *
44bad0eccaSFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45bad0eccaSFrançois Tigeot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46bad0eccaSFrançois Tigeot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47bad0eccaSFrançois Tigeot  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48bad0eccaSFrançois Tigeot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49bad0eccaSFrançois Tigeot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50bad0eccaSFrançois Tigeot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51bad0eccaSFrançois Tigeot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52bad0eccaSFrançois Tigeot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53bad0eccaSFrançois Tigeot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54bad0eccaSFrançois Tigeot  * SUCH DAMAGE.
55bad0eccaSFrançois Tigeot  */
56bad0eccaSFrançois Tigeot 
57bad0eccaSFrançois Tigeot #include <sys/mplock2.h>
58bad0eccaSFrançois Tigeot 
59a2fdbec6SFrançois Tigeot #include <linux/i2c.h>
60a2fdbec6SFrançois Tigeot #include <linux/export.h>
61bad0eccaSFrançois Tigeot #include <drm/drmP.h>
62a2fdbec6SFrançois Tigeot #include "intel_drv.h"
63bad0eccaSFrançois Tigeot #include <drm/i915_drm.h>
64bad0eccaSFrançois Tigeot #include "i915_drv.h"
65a2fdbec6SFrançois Tigeot 
66bad0eccaSFrançois Tigeot #include <bus/iicbus/iic.h>
67bad0eccaSFrançois Tigeot #include <bus/iicbus/iiconf.h>
68bad0eccaSFrançois Tigeot #include <bus/iicbus/iicbus.h>
69bad0eccaSFrançois Tigeot #include "iicbus_if.h"
70bad0eccaSFrançois Tigeot #include "iicbb_if.h"
71bad0eccaSFrançois Tigeot 
7219c468b4SFrançois Tigeot struct gmbus_pin {
73a2fdbec6SFrançois Tigeot 	const char *name;
74a2fdbec6SFrançois Tigeot 	int reg;
75a2fdbec6SFrançois Tigeot };
76a2fdbec6SFrançois Tigeot 
7719c468b4SFrançois Tigeot /* Map gmbus pin pairs to names and registers. */
7819c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins[] = {
7919c468b4SFrançois Tigeot 	[GMBUS_PIN_SSC] = { "ssc", GPIOB },
8019c468b4SFrançois Tigeot 	[GMBUS_PIN_VGADDC] = { "vga", GPIOA },
8119c468b4SFrançois Tigeot 	[GMBUS_PIN_PANEL] = { "panel", GPIOC },
8219c468b4SFrançois Tigeot 	[GMBUS_PIN_DPC] = { "dpc", GPIOD },
8319c468b4SFrançois Tigeot 	[GMBUS_PIN_DPB] = { "dpb", GPIOE },
8419c468b4SFrançois Tigeot 	[GMBUS_PIN_DPD] = { "dpd", GPIOF },
85a2fdbec6SFrançois Tigeot };
86bad0eccaSFrançois Tigeot 
8719c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins_bdw[] = {
8819c468b4SFrançois Tigeot 	[GMBUS_PIN_VGADDC] = { "vga", GPIOA },
8919c468b4SFrançois Tigeot 	[GMBUS_PIN_DPC] = { "dpc", GPIOD },
9019c468b4SFrançois Tigeot 	[GMBUS_PIN_DPB] = { "dpb", GPIOE },
9119c468b4SFrançois Tigeot 	[GMBUS_PIN_DPD] = { "dpd", GPIOF },
9219c468b4SFrançois Tigeot };
9319c468b4SFrançois Tigeot 
9419c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins_skl[] = {
9519c468b4SFrançois Tigeot 	[GMBUS_PIN_DPC] = { "dpc", GPIOD },
9619c468b4SFrançois Tigeot 	[GMBUS_PIN_DPB] = { "dpb", GPIOE },
9719c468b4SFrançois Tigeot 	[GMBUS_PIN_DPD] = { "dpd", GPIOF },
9819c468b4SFrançois Tigeot };
9919c468b4SFrançois Tigeot 
10019c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins_bxt[] = {
10119c468b4SFrançois Tigeot 	[GMBUS_PIN_1_BXT] = { "dpb", PCH_GPIOB },
10219c468b4SFrançois Tigeot 	[GMBUS_PIN_2_BXT] = { "dpc", PCH_GPIOC },
10319c468b4SFrançois Tigeot 	[GMBUS_PIN_3_BXT] = { "misc", PCH_GPIOD },
10419c468b4SFrançois Tigeot };
10519c468b4SFrançois Tigeot 
10619c468b4SFrançois Tigeot /* pin is expected to be valid */
10719c468b4SFrançois Tigeot static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
10819c468b4SFrançois Tigeot 					     unsigned int pin)
10919c468b4SFrançois Tigeot {
11019c468b4SFrançois Tigeot 	if (IS_BROXTON(dev_priv))
11119c468b4SFrançois Tigeot 		return &gmbus_pins_bxt[pin];
11219c468b4SFrançois Tigeot 	else if (IS_SKYLAKE(dev_priv))
11319c468b4SFrançois Tigeot 		return &gmbus_pins_skl[pin];
11419c468b4SFrançois Tigeot 	else if (IS_BROADWELL(dev_priv))
11519c468b4SFrançois Tigeot 		return &gmbus_pins_bdw[pin];
11619c468b4SFrançois Tigeot 	else
11719c468b4SFrançois Tigeot 		return &gmbus_pins[pin];
11819c468b4SFrançois Tigeot }
11919c468b4SFrançois Tigeot 
12019c468b4SFrançois Tigeot bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
12119c468b4SFrançois Tigeot 			      unsigned int pin)
12219c468b4SFrançois Tigeot {
12319c468b4SFrançois Tigeot 	unsigned int size;
12419c468b4SFrançois Tigeot 
12519c468b4SFrançois Tigeot 	if (IS_BROXTON(dev_priv))
12619c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins_bxt);
12719c468b4SFrançois Tigeot 	else if (IS_SKYLAKE(dev_priv))
12819c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins_skl);
12919c468b4SFrançois Tigeot 	else if (IS_BROADWELL(dev_priv))
13019c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins_bdw);
13119c468b4SFrançois Tigeot 	else
13219c468b4SFrançois Tigeot 		size = ARRAY_SIZE(gmbus_pins);
13319c468b4SFrançois Tigeot 
13419c468b4SFrançois Tigeot 	return pin < size && get_gmbus_pin(dev_priv, pin)->reg;
13519c468b4SFrançois Tigeot }
13619c468b4SFrançois Tigeot 
137bad0eccaSFrançois Tigeot /* Intel GPIO access functions */
138bad0eccaSFrançois Tigeot 
139bad0eccaSFrançois Tigeot #define I2C_RISEFALL_TIME 10
140bad0eccaSFrançois Tigeot 
141a2fdbec6SFrançois Tigeot void
142a2fdbec6SFrançois Tigeot intel_i2c_reset(struct drm_device *dev)
143a2fdbec6SFrançois Tigeot {
144a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1459edbd4a0SFrançois Tigeot 
146a2fdbec6SFrançois Tigeot 	I915_WRITE(dev_priv->gpio_mmio_base + GMBUS0, 0);
147a2fdbec6SFrançois Tigeot 	I915_WRITE(dev_priv->gpio_mmio_base + GMBUS4, 0);
148a2fdbec6SFrançois Tigeot }
149a2fdbec6SFrançois Tigeot 
150a2fdbec6SFrançois Tigeot static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
151bad0eccaSFrançois Tigeot {
152bad0eccaSFrançois Tigeot 	u32 val;
153bad0eccaSFrançois Tigeot 
154bad0eccaSFrançois Tigeot 	/* When using bit bashing for I2C, this bit needs to be set to 1 */
155bad0eccaSFrançois Tigeot 	if (!IS_PINEVIEW(dev_priv->dev))
156bad0eccaSFrançois Tigeot 		return;
157bad0eccaSFrançois Tigeot 
158bad0eccaSFrançois Tigeot 	val = I915_READ(DSPCLK_GATE_D);
159bad0eccaSFrançois Tigeot 	if (enable)
160bad0eccaSFrançois Tigeot 		val |= DPCUNIT_CLOCK_GATE_DISABLE;
161bad0eccaSFrançois Tigeot 	else
162bad0eccaSFrançois Tigeot 		val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
163bad0eccaSFrançois Tigeot 	I915_WRITE(DSPCLK_GATE_D, val);
164bad0eccaSFrançois Tigeot }
165bad0eccaSFrançois Tigeot 
166a2fdbec6SFrançois Tigeot static u32 get_reserved(device_t idev)
167a2fdbec6SFrançois Tigeot {
168a2fdbec6SFrançois Tigeot 	struct intel_iic_softc *sc = device_get_softc(idev);
169a2fdbec6SFrançois Tigeot 	struct drm_device *dev = sc->drm_dev;
170a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv;
171a2fdbec6SFrançois Tigeot 	u32 reserved = 0;
172a2fdbec6SFrançois Tigeot 
173a2fdbec6SFrançois Tigeot 	dev_priv = dev->dev_private;
174a2fdbec6SFrançois Tigeot 
175a2fdbec6SFrançois Tigeot 	/* On most chips, these bits must be preserved in software. */
176a2fdbec6SFrançois Tigeot 	if (!IS_I830(dev) && !IS_845G(dev))
177a2fdbec6SFrançois Tigeot 		reserved = I915_READ_NOTRACE(sc->reg) &
178a2fdbec6SFrançois Tigeot 					     (GPIO_DATA_PULLUP_DISABLE |
179a2fdbec6SFrançois Tigeot 					      GPIO_CLOCK_PULLUP_DISABLE);
180a2fdbec6SFrançois Tigeot 
181a2fdbec6SFrançois Tigeot 	return reserved;
182a2fdbec6SFrançois Tigeot }
183a2fdbec6SFrançois Tigeot 
184a2fdbec6SFrançois Tigeot static int get_clock(device_t idev)
185bad0eccaSFrançois Tigeot {
186bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
187bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
188bad0eccaSFrançois Tigeot 	u32 reserved;
189bad0eccaSFrançois Tigeot 
190bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
191a2fdbec6SFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
192bad0eccaSFrançois Tigeot 
193a2fdbec6SFrançois Tigeot 	reserved = get_reserved(idev);
194a2fdbec6SFrançois Tigeot 
195a2fdbec6SFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_CLOCK_DIR_MASK);
196a2fdbec6SFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved);
197a2fdbec6SFrançois Tigeot 	return ((I915_READ_NOTRACE(sc->reg) & GPIO_CLOCK_VAL_IN) != 0);
198bad0eccaSFrançois Tigeot }
199bad0eccaSFrançois Tigeot 
200a2fdbec6SFrançois Tigeot static int get_data(device_t idev)
201bad0eccaSFrançois Tigeot {
202a2fdbec6SFrançois Tigeot 	struct intel_iic_softc *sc;
203bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
204a2fdbec6SFrançois Tigeot 	u32 reserved;
205bad0eccaSFrançois Tigeot 
206a2fdbec6SFrançois Tigeot 	sc = device_get_softc(idev);
207a2fdbec6SFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
208a2fdbec6SFrançois Tigeot 
209a2fdbec6SFrançois Tigeot 	reserved = get_reserved(idev);
210a2fdbec6SFrançois Tigeot 
211a2fdbec6SFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_DATA_DIR_MASK);
212a2fdbec6SFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved);
213a2fdbec6SFrançois Tigeot 	return ((I915_READ_NOTRACE(sc->reg) & GPIO_DATA_VAL_IN) != 0);
214bad0eccaSFrançois Tigeot }
215bad0eccaSFrançois Tigeot 
216bad0eccaSFrançois Tigeot static int
217bad0eccaSFrançois Tigeot intel_iicbus_reset(device_t idev, u_char speed, u_char addr, u_char *oldaddr)
218bad0eccaSFrançois Tigeot {
219bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
220bad0eccaSFrançois Tigeot 	struct drm_device *dev;
221bad0eccaSFrançois Tigeot 
222bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
223bad0eccaSFrançois Tigeot 	dev = sc->drm_dev;
224bad0eccaSFrançois Tigeot 
2253f2f609dSFrançois Tigeot 	intel_i2c_reset(dev);
226bad0eccaSFrançois Tigeot 	return (0);
227bad0eccaSFrançois Tigeot }
228bad0eccaSFrançois Tigeot 
229a2fdbec6SFrançois Tigeot static void set_clock(device_t idev, int val)
230bad0eccaSFrançois Tigeot {
231bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
232bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
233bad0eccaSFrançois Tigeot 	u32 clock_bits, reserved;
234bad0eccaSFrançois Tigeot 
235bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
236bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
237bad0eccaSFrançois Tigeot 
238a2fdbec6SFrançois Tigeot 	reserved = get_reserved(idev);
239bad0eccaSFrançois Tigeot 	if (val)
240bad0eccaSFrançois Tigeot 		clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
241bad0eccaSFrançois Tigeot 	else
242bad0eccaSFrançois Tigeot 		clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
243bad0eccaSFrançois Tigeot 		    GPIO_CLOCK_VAL_MASK;
244bad0eccaSFrançois Tigeot 
245bad0eccaSFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved | clock_bits);
246bad0eccaSFrançois Tigeot 	POSTING_READ(sc->reg);
247bad0eccaSFrançois Tigeot }
248bad0eccaSFrançois Tigeot 
249a2fdbec6SFrançois Tigeot static void set_data(device_t idev, int val)
250bad0eccaSFrançois Tigeot {
251bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
252bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
253bad0eccaSFrançois Tigeot 	u32 reserved;
254a2fdbec6SFrançois Tigeot 	u32 data_bits;
255bad0eccaSFrançois Tigeot 
256bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
257bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
258bad0eccaSFrançois Tigeot 
259a2fdbec6SFrançois Tigeot 	reserved = get_reserved(idev);
26027a0f882SMatthew Dillon 
261a2fdbec6SFrançois Tigeot 	if (val)
262a2fdbec6SFrançois Tigeot 		data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
263a2fdbec6SFrançois Tigeot 	else
264a2fdbec6SFrançois Tigeot 		data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
265a2fdbec6SFrançois Tigeot 		    GPIO_DATA_VAL_MASK;
266bad0eccaSFrançois Tigeot 
267a2fdbec6SFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved | data_bits);
268a2fdbec6SFrançois Tigeot 	POSTING_READ(sc->reg);
269bad0eccaSFrançois Tigeot }
270bad0eccaSFrançois Tigeot 
27119c468b4SFrançois Tigeot static const char *gpio_names[GMBUS_NUM_PINS] = {
272a2fdbec6SFrançois Tigeot 	"ssc",
273a2fdbec6SFrançois Tigeot 	"vga",
274a2fdbec6SFrançois Tigeot 	"panel",
275a2fdbec6SFrançois Tigeot 	"dpc",
276a2fdbec6SFrançois Tigeot 	"dpb",
277a2fdbec6SFrançois Tigeot 	"dpd",
278a2fdbec6SFrançois Tigeot };
279a2fdbec6SFrançois Tigeot 
280bad0eccaSFrançois Tigeot static int
281a2fdbec6SFrançois Tigeot intel_gpio_setup(device_t idev)
282bad0eccaSFrançois Tigeot {
283a2fdbec6SFrançois Tigeot 	static const int map_pin_to_reg[] = {
284a2fdbec6SFrançois Tigeot 		0,
285a2fdbec6SFrançois Tigeot 		GPIOB,
286a2fdbec6SFrançois Tigeot 		GPIOA,
287a2fdbec6SFrançois Tigeot 		GPIOC,
288a2fdbec6SFrançois Tigeot 		GPIOD,
289a2fdbec6SFrançois Tigeot 		GPIOE,
290a2fdbec6SFrançois Tigeot 		GPIOF,
291a2fdbec6SFrançois Tigeot 		0
292a2fdbec6SFrançois Tigeot 	};
293a2fdbec6SFrançois Tigeot 
294bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
295bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
296a2fdbec6SFrançois Tigeot 	int pin;
297bad0eccaSFrançois Tigeot 
298bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
299a2fdbec6SFrançois Tigeot 	sc->drm_dev = device_get_softc(device_get_parent(idev));
300bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
301a2fdbec6SFrançois Tigeot 	pin = device_get_unit(idev);
302bad0eccaSFrançois Tigeot 
303a2fdbec6SFrançois Tigeot 	ksnprintf(sc->name, sizeof(sc->name), "i915 iicbb %s", gpio_names[pin]);
304a2fdbec6SFrançois Tigeot 	device_set_desc(idev, sc->name);
305bad0eccaSFrançois Tigeot 
30619c468b4SFrançois Tigeot 	sc->reg0 = pin | GMBUS_RATE_100KHZ;
30719c468b4SFrançois Tigeot 	sc->reg = dev_priv->gpio_mmio_base + map_pin_to_reg[pin];
308a2fdbec6SFrançois Tigeot 
309a2fdbec6SFrançois Tigeot 	/* add generic bit-banging code */
310a2fdbec6SFrançois Tigeot 	sc->iic_dev = device_add_child(idev, "iicbb", -1);
311a2fdbec6SFrançois Tigeot 	if (sc->iic_dev == NULL)
312a2fdbec6SFrançois Tigeot 		return (ENXIO);
313a2fdbec6SFrançois Tigeot 	device_quiet(sc->iic_dev);
314a2fdbec6SFrançois Tigeot 	bus_generic_attach(idev);
315a2fdbec6SFrançois Tigeot 
316a2fdbec6SFrançois Tigeot 	return (0);
317bad0eccaSFrançois Tigeot }
318bad0eccaSFrançois Tigeot 
319bad0eccaSFrançois Tigeot static int
320a2fdbec6SFrançois Tigeot intel_i2c_quirk_xfer(device_t idev, struct iic_msg *msgs, int nmsgs)
321bad0eccaSFrançois Tigeot {
322a2fdbec6SFrançois Tigeot 	device_t bridge_dev;
323bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
324bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
325a2fdbec6SFrançois Tigeot 	int ret;
326a2fdbec6SFrançois Tigeot 	int i;
327a2fdbec6SFrançois Tigeot 
328a2fdbec6SFrançois Tigeot 	bridge_dev = device_get_parent(device_get_parent(idev));
329a2fdbec6SFrançois Tigeot 	sc = device_get_softc(bridge_dev);
330a2fdbec6SFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
331a2fdbec6SFrançois Tigeot 
332a2fdbec6SFrançois Tigeot 	intel_i2c_reset(sc->drm_dev);
333a2fdbec6SFrançois Tigeot 	intel_i2c_quirk_set(dev_priv, true);
334a2fdbec6SFrançois Tigeot 	IICBB_SETSDA(bridge_dev, 1);
335a2fdbec6SFrançois Tigeot 	IICBB_SETSCL(bridge_dev, 1);
336a2fdbec6SFrançois Tigeot 	DELAY(I2C_RISEFALL_TIME);
337a2fdbec6SFrançois Tigeot 
338a2fdbec6SFrançois Tigeot 	for (i = 0; i < nmsgs - 1; i++) {
339a2fdbec6SFrançois Tigeot 		/* force use of repeated start instead of default stop+start */
340a2fdbec6SFrançois Tigeot 		msgs[i].flags |= IIC_M_NOSTOP;
341a2fdbec6SFrançois Tigeot 	}
342a2fdbec6SFrançois Tigeot 	ret = iicbus_transfer(idev, msgs, nmsgs);
343a2fdbec6SFrançois Tigeot 	IICBB_SETSDA(bridge_dev, 1);
344a2fdbec6SFrançois Tigeot 	IICBB_SETSCL(bridge_dev, 1);
345a2fdbec6SFrançois Tigeot 	intel_i2c_quirk_set(dev_priv, false);
346a2fdbec6SFrançois Tigeot 
347a2fdbec6SFrançois Tigeot 	return (ret);
348a2fdbec6SFrançois Tigeot }
349a2fdbec6SFrançois Tigeot 
350a2fdbec6SFrançois Tigeot static int
351a2fdbec6SFrançois Tigeot gmbus_wait_hw_status(struct drm_i915_private *dev_priv,
352a2fdbec6SFrançois Tigeot 		     u32 gmbus2_status,
353a2fdbec6SFrançois Tigeot 		     u32 gmbus4_irq_en)
354a2fdbec6SFrançois Tigeot {
355a2fdbec6SFrançois Tigeot 	int i;
356a2fdbec6SFrançois Tigeot 	int reg_offset = dev_priv->gpio_mmio_base;
357a2fdbec6SFrançois Tigeot 	u32 gmbus2 = 0;
358a2fdbec6SFrançois Tigeot 	DEFINE_WAIT(wait);
359a2fdbec6SFrançois Tigeot 
360a2fdbec6SFrançois Tigeot 	if (!HAS_GMBUS_IRQ(dev_priv->dev))
361a2fdbec6SFrançois Tigeot 		gmbus4_irq_en = 0;
362a2fdbec6SFrançois Tigeot 
363a2fdbec6SFrançois Tigeot 	/* Important: The hw handles only the first bit, so set only one! Since
364a2fdbec6SFrançois Tigeot 	 * we also need to check for NAKs besides the hw ready/idle signal, we
365a2fdbec6SFrançois Tigeot 	 * need to wake up periodically and check that ourselves. */
366a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS4 + reg_offset, gmbus4_irq_en);
367a2fdbec6SFrançois Tigeot 
3688e26cdf6SFrançois Tigeot 	for (i = 0; i < msecs_to_jiffies_timeout(50); i++) {
369a2fdbec6SFrançois Tigeot 		prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait,
370a2fdbec6SFrançois Tigeot 				TASK_UNINTERRUPTIBLE);
371a2fdbec6SFrançois Tigeot 
372a2fdbec6SFrançois Tigeot 		gmbus2 = I915_READ_NOTRACE(GMBUS2 + reg_offset);
373a2fdbec6SFrançois Tigeot 		if (gmbus2 & (GMBUS_SATOER | gmbus2_status))
374a2fdbec6SFrançois Tigeot 			break;
375a2fdbec6SFrançois Tigeot 
376a2fdbec6SFrançois Tigeot 		schedule_timeout(1);
377a2fdbec6SFrançois Tigeot 	}
378a2fdbec6SFrançois Tigeot 	finish_wait(&dev_priv->gmbus_wait_queue, &wait);
379a2fdbec6SFrançois Tigeot 
380a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS4 + reg_offset, 0);
381a2fdbec6SFrançois Tigeot 
382a2fdbec6SFrançois Tigeot 	if (gmbus2 & GMBUS_SATOER)
383a2fdbec6SFrançois Tigeot 		return -ENXIO;
384a2fdbec6SFrançois Tigeot 	if (gmbus2 & gmbus2_status)
385a2fdbec6SFrançois Tigeot 		return 0;
386a2fdbec6SFrançois Tigeot 	return -ETIMEDOUT;
387a2fdbec6SFrançois Tigeot }
388a2fdbec6SFrançois Tigeot 
389a2fdbec6SFrançois Tigeot static int
390a2fdbec6SFrançois Tigeot gmbus_wait_idle(struct drm_i915_private *dev_priv)
391a2fdbec6SFrançois Tigeot {
392a2fdbec6SFrançois Tigeot 	int ret;
393a2fdbec6SFrançois Tigeot 	int reg_offset = dev_priv->gpio_mmio_base;
394a2fdbec6SFrançois Tigeot 
395a2fdbec6SFrançois Tigeot #define C ((I915_READ_NOTRACE(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0)
396a2fdbec6SFrançois Tigeot 
397a2fdbec6SFrançois Tigeot 	if (!HAS_GMBUS_IRQ(dev_priv->dev))
398a2fdbec6SFrançois Tigeot 		return wait_for(C, 10);
399a2fdbec6SFrançois Tigeot 
400a2fdbec6SFrançois Tigeot 	/* Important: The hw handles only the first bit, so set only one! */
401a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS4 + reg_offset, GMBUS_IDLE_EN);
402a2fdbec6SFrançois Tigeot 
4038e26cdf6SFrançois Tigeot 	ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
4048e26cdf6SFrançois Tigeot 				 msecs_to_jiffies_timeout(10));
405a2fdbec6SFrançois Tigeot 
406a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS4 + reg_offset, 0);
407a2fdbec6SFrançois Tigeot 
408a2fdbec6SFrançois Tigeot 	if (ret)
409a2fdbec6SFrançois Tigeot 		return 0;
410a2fdbec6SFrançois Tigeot 	else
411a2fdbec6SFrançois Tigeot 		return -ETIMEDOUT;
412a2fdbec6SFrançois Tigeot #undef C
413a2fdbec6SFrançois Tigeot }
414a2fdbec6SFrançois Tigeot 
415a2fdbec6SFrançois Tigeot static int
416477eb7f9SFrançois Tigeot gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
417477eb7f9SFrançois Tigeot 		      unsigned short addr, u8 *buf, unsigned int len,
418a2fdbec6SFrançois Tigeot 		      u32 gmbus1_index)
419a2fdbec6SFrançois Tigeot {
420a2fdbec6SFrançois Tigeot 	int reg_offset = dev_priv->gpio_mmio_base;
421a2fdbec6SFrançois Tigeot 
422a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS1 + reg_offset,
423a2fdbec6SFrançois Tigeot 		   gmbus1_index |
424a2fdbec6SFrançois Tigeot 		   GMBUS_CYCLE_WAIT |
425a2fdbec6SFrançois Tigeot 		   (len << GMBUS_BYTE_COUNT_SHIFT) |
426477eb7f9SFrançois Tigeot 		   (addr << GMBUS_SLAVE_ADDR_SHIFT) |
427a2fdbec6SFrançois Tigeot 		   GMBUS_SLAVE_READ | GMBUS_SW_RDY);
428a2fdbec6SFrançois Tigeot 	while (len) {
429a2fdbec6SFrançois Tigeot 		int ret;
430a2fdbec6SFrançois Tigeot 		u32 val, loop = 0;
431a2fdbec6SFrançois Tigeot 
432a2fdbec6SFrançois Tigeot 		ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
433a2fdbec6SFrançois Tigeot 					   GMBUS_HW_RDY_EN);
434a2fdbec6SFrançois Tigeot 		if (ret)
435a2fdbec6SFrançois Tigeot 			return ret;
436a2fdbec6SFrançois Tigeot 
437a2fdbec6SFrançois Tigeot 		val = I915_READ(GMBUS3 + reg_offset);
438a2fdbec6SFrançois Tigeot 		do {
439a2fdbec6SFrançois Tigeot 			*buf++ = val & 0xff;
440a2fdbec6SFrançois Tigeot 			val >>= 8;
441a2fdbec6SFrançois Tigeot 		} while (--len && ++loop < 4);
442a2fdbec6SFrançois Tigeot 	}
443a2fdbec6SFrançois Tigeot 
444a2fdbec6SFrançois Tigeot 	return 0;
445a2fdbec6SFrançois Tigeot }
446a2fdbec6SFrançois Tigeot 
447a2fdbec6SFrançois Tigeot static int
448477eb7f9SFrançois Tigeot gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
449477eb7f9SFrançois Tigeot 		u32 gmbus1_index)
450477eb7f9SFrançois Tigeot {
451477eb7f9SFrançois Tigeot 	u8 *buf = msg->buf;
452477eb7f9SFrançois Tigeot 	unsigned int rx_size = msg->len;
453477eb7f9SFrançois Tigeot 	unsigned int len;
454477eb7f9SFrançois Tigeot 	int ret;
455477eb7f9SFrançois Tigeot 
456477eb7f9SFrançois Tigeot 	do {
457477eb7f9SFrançois Tigeot 		len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
458477eb7f9SFrançois Tigeot 
459477eb7f9SFrançois Tigeot 		ret = gmbus_xfer_read_chunk(dev_priv, msg->slave >> 1,
460477eb7f9SFrançois Tigeot 					    buf, len, gmbus1_index);
461477eb7f9SFrançois Tigeot 		if (ret)
462477eb7f9SFrançois Tigeot 			return ret;
463477eb7f9SFrançois Tigeot 
464477eb7f9SFrançois Tigeot 		rx_size -= len;
465477eb7f9SFrançois Tigeot 		buf += len;
466477eb7f9SFrançois Tigeot 	} while (rx_size != 0);
467477eb7f9SFrançois Tigeot 
468477eb7f9SFrançois Tigeot 	return 0;
469477eb7f9SFrançois Tigeot }
470477eb7f9SFrançois Tigeot 
471477eb7f9SFrançois Tigeot static int
472477eb7f9SFrançois Tigeot gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
473477eb7f9SFrançois Tigeot 		       unsigned short addr, u8 *buf, unsigned int len)
474a2fdbec6SFrançois Tigeot {
475a2fdbec6SFrançois Tigeot 	int reg_offset = dev_priv->gpio_mmio_base;
476477eb7f9SFrançois Tigeot 	unsigned int chunk_size = len;
477bad0eccaSFrançois Tigeot 	u32 val, loop;
478bad0eccaSFrançois Tigeot 
479a2fdbec6SFrançois Tigeot 	val = loop = 0;
480a2fdbec6SFrançois Tigeot 	while (len && loop < 4) {
481a2fdbec6SFrançois Tigeot 		val |= *buf++ << (8 * loop++);
482a2fdbec6SFrançois Tigeot 		len -= 1;
483a2fdbec6SFrançois Tigeot 	}
484a2fdbec6SFrançois Tigeot 
485a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS3 + reg_offset, val);
486a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS1 + reg_offset,
487a2fdbec6SFrançois Tigeot 		   GMBUS_CYCLE_WAIT |
488477eb7f9SFrançois Tigeot 		   (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
489477eb7f9SFrançois Tigeot 		   (addr << GMBUS_SLAVE_ADDR_SHIFT) |
490a2fdbec6SFrançois Tigeot 		   GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
491a2fdbec6SFrançois Tigeot 	while (len) {
492a2fdbec6SFrançois Tigeot 		int ret;
493a2fdbec6SFrançois Tigeot 
494a2fdbec6SFrançois Tigeot 		val = loop = 0;
495a2fdbec6SFrançois Tigeot 		do {
496a2fdbec6SFrançois Tigeot 			val |= *buf++ << (8 * loop);
497a2fdbec6SFrançois Tigeot 		} while (--len && ++loop < 4);
498a2fdbec6SFrançois Tigeot 
499a2fdbec6SFrançois Tigeot 		I915_WRITE(GMBUS3 + reg_offset, val);
500a2fdbec6SFrançois Tigeot 
501a2fdbec6SFrançois Tigeot 		ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
502a2fdbec6SFrançois Tigeot 					   GMBUS_HW_RDY_EN);
503a2fdbec6SFrançois Tigeot 		if (ret)
504a2fdbec6SFrançois Tigeot 			return ret;
505a2fdbec6SFrançois Tigeot 	}
506477eb7f9SFrançois Tigeot 
507477eb7f9SFrançois Tigeot 	return 0;
508477eb7f9SFrançois Tigeot }
509477eb7f9SFrançois Tigeot 
510477eb7f9SFrançois Tigeot static int
511477eb7f9SFrançois Tigeot gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
512477eb7f9SFrançois Tigeot {
513477eb7f9SFrançois Tigeot 	u8 *buf = msg->buf;
514477eb7f9SFrançois Tigeot 	unsigned int tx_size = msg->len;
515477eb7f9SFrançois Tigeot 	unsigned int len;
516477eb7f9SFrançois Tigeot 	int ret;
517477eb7f9SFrançois Tigeot 
518477eb7f9SFrançois Tigeot 	do {
519477eb7f9SFrançois Tigeot 		len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
520477eb7f9SFrançois Tigeot 
521477eb7f9SFrançois Tigeot 		ret = gmbus_xfer_write_chunk(dev_priv, msg->slave >> 1, buf, len);
522477eb7f9SFrançois Tigeot 		if (ret)
523477eb7f9SFrançois Tigeot 			return ret;
524477eb7f9SFrançois Tigeot 
525477eb7f9SFrançois Tigeot 		buf += len;
526477eb7f9SFrançois Tigeot 		tx_size -= len;
527477eb7f9SFrançois Tigeot 	} while (tx_size != 0);
528477eb7f9SFrançois Tigeot 
529a2fdbec6SFrançois Tigeot 	return 0;
530a2fdbec6SFrançois Tigeot }
531a2fdbec6SFrançois Tigeot 
532a2fdbec6SFrançois Tigeot /*
533a2fdbec6SFrançois Tigeot  * The gmbus controller can combine a 1 or 2 byte write with a read that
534a2fdbec6SFrançois Tigeot  * immediately follows it by using an "INDEX" cycle.
535a2fdbec6SFrançois Tigeot  */
536a2fdbec6SFrançois Tigeot static bool
537a2fdbec6SFrançois Tigeot gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
538a2fdbec6SFrançois Tigeot {
539a2fdbec6SFrançois Tigeot 	return (i + 1 < num &&
540a2fdbec6SFrançois Tigeot 		!(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
541a2fdbec6SFrançois Tigeot 		(msgs[i + 1].flags & I2C_M_RD));
542a2fdbec6SFrançois Tigeot }
543a2fdbec6SFrançois Tigeot 
544a2fdbec6SFrançois Tigeot static int
545a2fdbec6SFrançois Tigeot gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
546a2fdbec6SFrançois Tigeot {
547a2fdbec6SFrançois Tigeot 	int reg_offset = dev_priv->gpio_mmio_base;
548a2fdbec6SFrançois Tigeot 	u32 gmbus1_index = 0;
549a2fdbec6SFrançois Tigeot 	u32 gmbus5 = 0;
550a2fdbec6SFrançois Tigeot 	int ret;
551a2fdbec6SFrançois Tigeot 
552a2fdbec6SFrançois Tigeot 	if (msgs[0].len == 2)
553a2fdbec6SFrançois Tigeot 		gmbus5 = GMBUS_2BYTE_INDEX_EN |
554a2fdbec6SFrançois Tigeot 			 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
555a2fdbec6SFrançois Tigeot 	if (msgs[0].len == 1)
556a2fdbec6SFrançois Tigeot 		gmbus1_index = GMBUS_CYCLE_INDEX |
557a2fdbec6SFrançois Tigeot 			       (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
558a2fdbec6SFrançois Tigeot 
559a2fdbec6SFrançois Tigeot 	/* GMBUS5 holds 16-bit index */
560a2fdbec6SFrançois Tigeot 	if (gmbus5)
561a2fdbec6SFrançois Tigeot 		I915_WRITE(GMBUS5 + reg_offset, gmbus5);
562a2fdbec6SFrançois Tigeot 
563a2fdbec6SFrançois Tigeot 	ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
564a2fdbec6SFrançois Tigeot 
565a2fdbec6SFrançois Tigeot 	/* Clear GMBUS5 after each index transfer */
566a2fdbec6SFrançois Tigeot 	if (gmbus5)
567a2fdbec6SFrançois Tigeot 		I915_WRITE(GMBUS5 + reg_offset, 0);
568a2fdbec6SFrançois Tigeot 
569a2fdbec6SFrançois Tigeot 	return ret;
570a2fdbec6SFrançois Tigeot }
571a2fdbec6SFrançois Tigeot 
572a2fdbec6SFrançois Tigeot static int
573a2fdbec6SFrançois Tigeot gmbus_xfer(struct device *adapter,
574a2fdbec6SFrançois Tigeot 	   struct i2c_msg *msgs,
575a2fdbec6SFrançois Tigeot 	   int num)
576a2fdbec6SFrançois Tigeot {
577a2fdbec6SFrançois Tigeot 	struct intel_iic_softc *sc;
578a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv;
579477eb7f9SFrançois Tigeot 	int i = 0, inc, try = 0, reg_offset;
580477eb7f9SFrançois Tigeot 	int unit;
581a2fdbec6SFrançois Tigeot 	int ret = 0;
582a2fdbec6SFrançois Tigeot 
583a2fdbec6SFrançois Tigeot 	sc = device_get_softc(adapter);
584bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
585a2fdbec6SFrançois Tigeot 	unit = device_get_unit(adapter);
586bad0eccaSFrançois Tigeot 
587a2fdbec6SFrançois Tigeot 	mutex_lock(&dev_priv->gmbus_mutex);
588a2fdbec6SFrançois Tigeot 
589bad0eccaSFrançois Tigeot 	if (sc->force_bit_dev) {
590a2fdbec6SFrançois Tigeot 		ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num);
591bad0eccaSFrançois Tigeot 		goto out;
592bad0eccaSFrançois Tigeot 	}
593bad0eccaSFrançois Tigeot 
594bad0eccaSFrançois Tigeot 	reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0;
595bad0eccaSFrançois Tigeot 
596477eb7f9SFrançois Tigeot retry:
597bad0eccaSFrançois Tigeot 	I915_WRITE(GMBUS0 + reg_offset, sc->reg0);
598bad0eccaSFrançois Tigeot 
599477eb7f9SFrançois Tigeot 	for (; i < num; i += inc) {
600477eb7f9SFrançois Tigeot 		inc = 1;
601a2fdbec6SFrançois Tigeot 		if (gmbus_is_index_read(msgs, i, num)) {
602a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
603477eb7f9SFrançois Tigeot 			inc = 2; /* an index read is two msgs */
604a2fdbec6SFrançois Tigeot 		} else if (msgs[i].flags & I2C_M_RD) {
605a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
606bad0eccaSFrançois Tigeot 		} else {
607a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_write(dev_priv, &msgs[i]);
608a2fdbec6SFrançois Tigeot 		}
609bad0eccaSFrançois Tigeot 
610a2fdbec6SFrançois Tigeot 		if (ret == -ETIMEDOUT)
611bad0eccaSFrançois Tigeot 			goto timeout;
612a2fdbec6SFrançois Tigeot 		if (ret == -ENXIO)
613bad0eccaSFrançois Tigeot 			goto clear_err;
614bad0eccaSFrançois Tigeot 
615a2fdbec6SFrançois Tigeot 		ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE,
616a2fdbec6SFrançois Tigeot 					   GMBUS_HW_WAIT_EN);
617a2fdbec6SFrançois Tigeot 		if (ret == -ENXIO)
618bad0eccaSFrançois Tigeot 			goto clear_err;
619a2fdbec6SFrançois Tigeot 		if (ret)
620a2fdbec6SFrançois Tigeot 			goto timeout;
621bad0eccaSFrançois Tigeot 	}
622bad0eccaSFrançois Tigeot 
623a2fdbec6SFrançois Tigeot 	/* Generate a STOP condition on the bus. Note that gmbus can't generata
624a2fdbec6SFrançois Tigeot 	 * a STOP on the very first cycle. To simplify the code we
625a2fdbec6SFrançois Tigeot 	 * unconditionally generate the STOP condition with an additional gmbus
626a2fdbec6SFrançois Tigeot 	 * cycle. */
627a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
628a2fdbec6SFrançois Tigeot 
629bad0eccaSFrançois Tigeot 	/* Mark the GMBUS interface as disabled after waiting for idle.
630bad0eccaSFrançois Tigeot 	 * We will re-enable it at the start of the next xfer,
631bad0eccaSFrançois Tigeot 	 * till then let it sleep.
632bad0eccaSFrançois Tigeot 	 */
633a2fdbec6SFrançois Tigeot 	if (gmbus_wait_idle(dev_priv)) {
634a2fdbec6SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
635a2fdbec6SFrançois Tigeot 			 sc->name);
636a2fdbec6SFrançois Tigeot 		ret = -ETIMEDOUT;
637a2fdbec6SFrançois Tigeot 	}
638bad0eccaSFrançois Tigeot 	I915_WRITE(GMBUS0 + reg_offset, 0);
639a2fdbec6SFrançois Tigeot 	ret = ret ?: i;
640a2fdbec6SFrançois Tigeot 	goto timeout;	/* XXX: should be out */
641bad0eccaSFrançois Tigeot 
642bad0eccaSFrançois Tigeot clear_err:
643a2fdbec6SFrançois Tigeot 	/*
644a2fdbec6SFrançois Tigeot 	 * Wait for bus to IDLE before clearing NAK.
645a2fdbec6SFrançois Tigeot 	 * If we clear the NAK while bus is still active, then it will stay
646a2fdbec6SFrançois Tigeot 	 * active and the next transaction may fail.
647a2fdbec6SFrançois Tigeot 	 *
648a2fdbec6SFrançois Tigeot 	 * If no ACK is received during the address phase of a transaction, the
649a2fdbec6SFrançois Tigeot 	 * adapter must report -ENXIO. It is not clear what to return if no ACK
650a2fdbec6SFrançois Tigeot 	 * is received at other times. But we have to be careful to not return
651a2fdbec6SFrançois Tigeot 	 * spurious -ENXIO because that will prevent i2c and drm edid functions
652a2fdbec6SFrançois Tigeot 	 * from retrying. So return -ENXIO only when gmbus properly quiescents -
653a2fdbec6SFrançois Tigeot 	 * timing out seems to happen when there _is_ a ddc chip present, but
654a2fdbec6SFrançois Tigeot 	 * it's slow responding and only answers on the 2nd retry.
655a2fdbec6SFrançois Tigeot 	 */
656a2fdbec6SFrançois Tigeot 	ret = -ENXIO;
657a2fdbec6SFrançois Tigeot 	if (gmbus_wait_idle(dev_priv)) {
658a2fdbec6SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
659a2fdbec6SFrançois Tigeot 			      sc->name);
660a2fdbec6SFrançois Tigeot 		ret = -ETIMEDOUT;
661a2fdbec6SFrançois Tigeot 	}
662a2fdbec6SFrançois Tigeot 
663bad0eccaSFrançois Tigeot 	/* Toggle the Software Clear Interrupt bit. This has the effect
664bad0eccaSFrançois Tigeot 	 * of resetting the GMBUS controller and so clearing the
665bad0eccaSFrançois Tigeot 	 * BUS_ERROR raised by the slave's NAK.
666bad0eccaSFrançois Tigeot 	 */
667bad0eccaSFrançois Tigeot 	I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
668bad0eccaSFrançois Tigeot 	I915_WRITE(GMBUS1 + reg_offset, 0);
669bad0eccaSFrançois Tigeot 	I915_WRITE(GMBUS0 + reg_offset, 0);
670bad0eccaSFrançois Tigeot 
671a2fdbec6SFrançois Tigeot 	DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
672a2fdbec6SFrançois Tigeot 			 sc->name, msgs[i].slave,
673a2fdbec6SFrançois Tigeot 			 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
674bad0eccaSFrançois Tigeot 
675477eb7f9SFrançois Tigeot 	/*
676477eb7f9SFrançois Tigeot 	 * Passive adapters sometimes NAK the first probe. Retry the first
677477eb7f9SFrançois Tigeot 	 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
678477eb7f9SFrançois Tigeot 	 * has retries internally. See also the retry loop in
679477eb7f9SFrançois Tigeot 	 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
680477eb7f9SFrançois Tigeot 	 */
681477eb7f9SFrançois Tigeot 	if (ret == -ENXIO && i == 0 && try++ == 0) {
682477eb7f9SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
683477eb7f9SFrançois Tigeot 			      sc->name);
684477eb7f9SFrançois Tigeot 		goto retry;
685477eb7f9SFrançois Tigeot 	}
686477eb7f9SFrançois Tigeot 
687bad0eccaSFrançois Tigeot 	goto out;
688a2fdbec6SFrançois Tigeot 
689a2fdbec6SFrançois Tigeot timeout:
690a2fdbec6SFrançois Tigeot 	DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
691a2fdbec6SFrançois Tigeot 		 sc->name, sc->reg0 & 0xff);
692a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS0 + reg_offset, 0);
693a2fdbec6SFrançois Tigeot 
694a2fdbec6SFrançois Tigeot 	/* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
695a2fdbec6SFrançois Tigeot 	sc->force_bit_dev = true;
696a2fdbec6SFrançois Tigeot 	ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num);
697a2fdbec6SFrançois Tigeot 
698a2fdbec6SFrançois Tigeot out:
699a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev_priv->gmbus_mutex);
700a2fdbec6SFrançois Tigeot 	return ret;
701bad0eccaSFrançois Tigeot }
702bad0eccaSFrançois Tigeot 
70319c468b4SFrançois Tigeot struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
70419c468b4SFrançois Tigeot 					    unsigned int pin)
70519df918dSFrançois Tigeot {
70619c468b4SFrançois Tigeot 	if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
70719c468b4SFrançois Tigeot 		return NULL;
70819c468b4SFrançois Tigeot 
70919c468b4SFrançois Tigeot 	return dev_priv->gmbus[pin];
71019df918dSFrançois Tigeot }
71119df918dSFrançois Tigeot 
712bad0eccaSFrançois Tigeot void
713bad0eccaSFrançois Tigeot intel_gmbus_set_speed(device_t idev, int speed)
714bad0eccaSFrançois Tigeot {
715bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
716bad0eccaSFrançois Tigeot 
717bad0eccaSFrançois Tigeot 	sc = device_get_softc(device_get_parent(idev));
718bad0eccaSFrançois Tigeot 
719bad0eccaSFrançois Tigeot 	sc->reg0 = (sc->reg0 & ~(0x3 << 8)) | speed;
720bad0eccaSFrançois Tigeot }
721bad0eccaSFrançois Tigeot 
722bad0eccaSFrançois Tigeot void
723bad0eccaSFrançois Tigeot intel_gmbus_force_bit(device_t idev, bool force_bit)
724bad0eccaSFrançois Tigeot {
725bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
726bad0eccaSFrançois Tigeot 
727bad0eccaSFrançois Tigeot 	sc = device_get_softc(device_get_parent(idev));
728a2fdbec6SFrançois Tigeot 	sc->force_bit_dev += force_bit ? 1 : -1;
729a2fdbec6SFrançois Tigeot 	DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
730a2fdbec6SFrançois Tigeot 		      force_bit ? "en" : "dis", sc->name,
731a2fdbec6SFrançois Tigeot 		      sc->force_bit_dev);
732bad0eccaSFrançois Tigeot }
733bad0eccaSFrançois Tigeot 
734bad0eccaSFrançois Tigeot static int
735bad0eccaSFrançois Tigeot intel_gmbus_probe(device_t dev)
736bad0eccaSFrançois Tigeot {
737bad0eccaSFrançois Tigeot 
738bad0eccaSFrançois Tigeot 	return (BUS_PROBE_SPECIFIC);
739bad0eccaSFrançois Tigeot }
740bad0eccaSFrançois Tigeot 
741bad0eccaSFrançois Tigeot static int
742bad0eccaSFrançois Tigeot intel_gmbus_attach(device_t idev)
743bad0eccaSFrançois Tigeot {
744bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
745bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
746bad0eccaSFrançois Tigeot 	int pin;
747bad0eccaSFrançois Tigeot 
748bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
749bad0eccaSFrançois Tigeot 	sc->drm_dev = device_get_softc(device_get_parent(idev));
750bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
751bad0eccaSFrançois Tigeot 	pin = device_get_unit(idev);
752bad0eccaSFrançois Tigeot 
753bad0eccaSFrançois Tigeot 	ksnprintf(sc->name, sizeof(sc->name), "gmbus bus %s", gpio_names[pin]);
754bad0eccaSFrançois Tigeot 	device_set_desc(idev, sc->name);
755bad0eccaSFrançois Tigeot 
756bad0eccaSFrançois Tigeot 	/* By default use a conservative clock rate */
75719c468b4SFrançois Tigeot 	sc->reg0 = pin | GMBUS_RATE_100KHZ;
758bad0eccaSFrançois Tigeot 
759bad0eccaSFrançois Tigeot 	/* XXX force bit banging until GMBUS is fully debugged */
760bad0eccaSFrançois Tigeot 	if (IS_GEN2(sc->drm_dev)) {
761bad0eccaSFrançois Tigeot 		sc->force_bit_dev = true;
762bad0eccaSFrançois Tigeot 	}
763bad0eccaSFrançois Tigeot 
764bad0eccaSFrançois Tigeot 	/* add bus interface device */
765bad0eccaSFrançois Tigeot 	sc->iic_dev = device_add_child(idev, "iicbus", -1);
766bad0eccaSFrançois Tigeot 	if (sc->iic_dev == NULL)
767bad0eccaSFrançois Tigeot 		return (ENXIO);
768bad0eccaSFrançois Tigeot 	device_quiet(sc->iic_dev);
769bad0eccaSFrançois Tigeot 	bus_generic_attach(idev);
770bad0eccaSFrançois Tigeot 
771bad0eccaSFrançois Tigeot 	return (0);
772bad0eccaSFrançois Tigeot }
773bad0eccaSFrançois Tigeot 
774bad0eccaSFrançois Tigeot static int
775bad0eccaSFrançois Tigeot intel_gmbus_detach(device_t idev)
776bad0eccaSFrançois Tigeot {
777bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
778bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
779bad0eccaSFrançois Tigeot 	device_t child;
780bad0eccaSFrançois Tigeot 	int u;
781bad0eccaSFrançois Tigeot 
782bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
783bad0eccaSFrançois Tigeot 	u = device_get_unit(idev);
784bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
785bad0eccaSFrançois Tigeot 
786bad0eccaSFrançois Tigeot 	child = sc->iic_dev;
787bad0eccaSFrançois Tigeot 	bus_generic_detach(idev);
788bad0eccaSFrançois Tigeot 	if (child != NULL)
789bad0eccaSFrançois Tigeot 		device_delete_child(idev, child);
790bad0eccaSFrançois Tigeot 
791bad0eccaSFrançois Tigeot 	return (0);
792bad0eccaSFrançois Tigeot }
793bad0eccaSFrançois Tigeot 
794bad0eccaSFrançois Tigeot static int
795bad0eccaSFrançois Tigeot intel_iicbb_probe(device_t dev)
796bad0eccaSFrançois Tigeot {
797bad0eccaSFrançois Tigeot 
798bad0eccaSFrançois Tigeot 	return (BUS_PROBE_DEFAULT);
799bad0eccaSFrançois Tigeot }
800bad0eccaSFrançois Tigeot 
801bad0eccaSFrançois Tigeot static int
802bad0eccaSFrançois Tigeot intel_iicbb_detach(device_t idev)
803bad0eccaSFrançois Tigeot {
804bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
805bad0eccaSFrançois Tigeot 	device_t child;
806bad0eccaSFrançois Tigeot 
807bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
808bad0eccaSFrançois Tigeot 	child = sc->iic_dev;
809bad0eccaSFrançois Tigeot 	bus_generic_detach(idev);
810bad0eccaSFrançois Tigeot 	if (child)
811bad0eccaSFrançois Tigeot 		device_delete_child(idev, child);
812bad0eccaSFrançois Tigeot 	return (0);
813bad0eccaSFrançois Tigeot }
814bad0eccaSFrançois Tigeot 
815bad0eccaSFrançois Tigeot static device_method_t intel_gmbus_methods[] = {
816bad0eccaSFrançois Tigeot 	DEVMETHOD(device_probe,		intel_gmbus_probe),
817bad0eccaSFrançois Tigeot 	DEVMETHOD(device_attach,	intel_gmbus_attach),
818bad0eccaSFrançois Tigeot 	DEVMETHOD(device_detach,	intel_gmbus_detach),
819bad0eccaSFrançois Tigeot 	DEVMETHOD(iicbus_reset,		intel_iicbus_reset),
820a2fdbec6SFrançois Tigeot 	DEVMETHOD(iicbus_transfer,	gmbus_xfer),
821bad0eccaSFrançois Tigeot 	DEVMETHOD_END
822bad0eccaSFrançois Tigeot };
823bad0eccaSFrançois Tigeot static driver_t intel_gmbus_driver = {
824bad0eccaSFrançois Tigeot 	"intel_gmbus",
825bad0eccaSFrançois Tigeot 	intel_gmbus_methods,
826bad0eccaSFrançois Tigeot 	sizeof(struct intel_iic_softc)
827bad0eccaSFrançois Tigeot };
828bad0eccaSFrançois Tigeot static devclass_t intel_gmbus_devclass;
829bad0eccaSFrançois Tigeot DRIVER_MODULE_ORDERED(intel_gmbus, drm, intel_gmbus_driver,
830aa6ac96eSSascha Wildner     intel_gmbus_devclass, NULL, NULL, SI_ORDER_FIRST);
8313a25be87SSascha Wildner DRIVER_MODULE(iicbus, intel_gmbus, iicbus_driver, iicbus_devclass, NULL, NULL);
832bad0eccaSFrançois Tigeot 
833bad0eccaSFrançois Tigeot static device_method_t intel_iicbb_methods[] =	{
834bad0eccaSFrançois Tigeot 	DEVMETHOD(device_probe,		intel_iicbb_probe),
835a2fdbec6SFrançois Tigeot 	DEVMETHOD(device_attach,	intel_gpio_setup),
836bad0eccaSFrançois Tigeot 	DEVMETHOD(device_detach,	intel_iicbb_detach),
837bad0eccaSFrançois Tigeot 
838bad0eccaSFrançois Tigeot 	DEVMETHOD(bus_add_child,	bus_generic_add_child),
839bad0eccaSFrançois Tigeot 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
840bad0eccaSFrançois Tigeot 
841bad0eccaSFrançois Tigeot 	DEVMETHOD(iicbb_callback,	iicbus_null_callback),
842bad0eccaSFrançois Tigeot 	DEVMETHOD(iicbb_reset,		intel_iicbus_reset),
843a2fdbec6SFrançois Tigeot 	DEVMETHOD(iicbb_setsda,		set_data),
844a2fdbec6SFrançois Tigeot 	DEVMETHOD(iicbb_setscl,		set_clock),
845a2fdbec6SFrançois Tigeot 	DEVMETHOD(iicbb_getsda,		get_data),
846a2fdbec6SFrançois Tigeot 	DEVMETHOD(iicbb_getscl,		get_clock),
847bad0eccaSFrançois Tigeot 	DEVMETHOD_END
848bad0eccaSFrançois Tigeot };
849bad0eccaSFrançois Tigeot static driver_t intel_iicbb_driver = {
850bad0eccaSFrançois Tigeot 	"intel_iicbb",
851bad0eccaSFrançois Tigeot 	intel_iicbb_methods,
852bad0eccaSFrançois Tigeot 	sizeof(struct intel_iic_softc)
853bad0eccaSFrançois Tigeot };
854bad0eccaSFrançois Tigeot static devclass_t intel_iicbb_devclass;
855bad0eccaSFrançois Tigeot DRIVER_MODULE_ORDERED(intel_iicbb, drm, intel_iicbb_driver,
856aa6ac96eSSascha Wildner     intel_iicbb_devclass, NULL, NULL, SI_ORDER_FIRST);
8573a25be87SSascha Wildner DRIVER_MODULE(iicbb, intel_iicbb, iicbb_driver, iicbb_devclass, NULL, NULL);
858bad0eccaSFrançois Tigeot 
859a2fdbec6SFrançois Tigeot static void intel_teardown_gmbus_m(struct drm_device *dev, int m);
860a2fdbec6SFrançois Tigeot 
86119c468b4SFrançois Tigeot /**
86219c468b4SFrançois Tigeot  * intel_gmbus_setup - instantiate all Intel i2c GMBuses
86319c468b4SFrançois Tigeot  * @dev: DRM device
86419c468b4SFrançois Tigeot  */
86519c468b4SFrançois Tigeot int intel_setup_gmbus(struct drm_device *dev)
866bad0eccaSFrançois Tigeot {
867a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
868bad0eccaSFrançois Tigeot 	device_t iic_dev;
86919c468b4SFrançois Tigeot 	unsigned int pin;
87019c468b4SFrançois Tigeot 	int ret;
871bad0eccaSFrançois Tigeot 
8728e26cdf6SFrançois Tigeot 	if (HAS_PCH_NOP(dev))
8738e26cdf6SFrançois Tigeot 		return 0;
8748e26cdf6SFrançois Tigeot 	else if (HAS_PCH_SPLIT(dev))
875a2fdbec6SFrançois Tigeot 		dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
876a2fdbec6SFrançois Tigeot 	else if (IS_VALLEYVIEW(dev))
877a2fdbec6SFrançois Tigeot 		dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
878a2fdbec6SFrançois Tigeot 	else
879a2fdbec6SFrançois Tigeot 		dev_priv->gpio_mmio_base = 0;
880a2fdbec6SFrançois Tigeot 
88119df918dSFrançois Tigeot 	lockinit(&dev_priv->gmbus_mutex, "gmbus", 0, LK_CANRECURSE);
882a2fdbec6SFrançois Tigeot 	init_waitqueue_head(&dev_priv->gmbus_wait_queue);
883a2fdbec6SFrançois Tigeot 
88419c468b4SFrançois Tigeot 	dev_priv->gmbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS,
8855a3b77d5SFrançois Tigeot 	    M_DRM, M_WAITOK | M_ZERO);
88619c468b4SFrançois Tigeot 	dev_priv->bbbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS,
8875a3b77d5SFrançois Tigeot 	    M_DRM, M_WAITOK | M_ZERO);
88819c468b4SFrançois Tigeot 	dev_priv->gmbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS,
8895a3b77d5SFrançois Tigeot 	    M_DRM, M_WAITOK | M_ZERO);
89019c468b4SFrançois Tigeot 	dev_priv->bbbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS,
8915a3b77d5SFrançois Tigeot 	    M_DRM, M_WAITOK | M_ZERO);
892bad0eccaSFrançois Tigeot 
89319c468b4SFrançois Tigeot 	for (pin = 0; pin < GMBUS_NUM_PINS; pin++) {
89419c468b4SFrançois Tigeot 		if (!intel_gmbus_is_valid_pin(dev_priv, pin))
89519c468b4SFrançois Tigeot 			continue;
89619c468b4SFrançois Tigeot 
897bad0eccaSFrançois Tigeot 		/*
898bad0eccaSFrançois Tigeot 		 * Initialized bbbus_bridge before gmbus_bridge, since
899bad0eccaSFrançois Tigeot 		 * gmbus may decide to force quirk transfer in the
900bad0eccaSFrançois Tigeot 		 * attachment code.
901bad0eccaSFrançois Tigeot 		 */
90219c468b4SFrançois Tigeot 		dev_priv->bbbus_bridge[pin] = device_add_child(dev->dev,
90319c468b4SFrançois Tigeot 		    "intel_iicbb", pin);
90419c468b4SFrançois Tigeot 		if (dev_priv->bbbus_bridge[pin] == NULL) {
90519c468b4SFrançois Tigeot 			DRM_ERROR("bbbus bridge %d creation failed\n", pin);
906bad0eccaSFrançois Tigeot 			ret = ENXIO;
907bad0eccaSFrançois Tigeot 			goto err;
908bad0eccaSFrançois Tigeot 		}
90919c468b4SFrançois Tigeot 		device_quiet(dev_priv->bbbus_bridge[pin]);
91019c468b4SFrançois Tigeot 		ret = device_probe_and_attach(dev_priv->bbbus_bridge[pin]);
911bad0eccaSFrançois Tigeot 		if (ret != 0) {
91219c468b4SFrançois Tigeot 			DRM_ERROR("bbbus bridge %d attach failed, %d\n", pin, ret);
913bad0eccaSFrançois Tigeot 			goto err;
914bad0eccaSFrançois Tigeot 		}
915bad0eccaSFrançois Tigeot 
91619c468b4SFrançois Tigeot 		iic_dev = device_find_child(dev_priv->bbbus_bridge[pin], "iicbb",
917bad0eccaSFrançois Tigeot 		    -1);
918bad0eccaSFrançois Tigeot 		if (iic_dev == NULL) {
919bad0eccaSFrançois Tigeot 			DRM_ERROR("bbbus bridge doesn't have iicbb child\n");
920bad0eccaSFrançois Tigeot 			goto err;
921bad0eccaSFrançois Tigeot 		}
922bad0eccaSFrançois Tigeot 		iic_dev = device_find_child(iic_dev, "iicbus", -1);
923bad0eccaSFrançois Tigeot 		if (iic_dev == NULL) {
924bad0eccaSFrançois Tigeot 			DRM_ERROR(
925bad0eccaSFrançois Tigeot 		"bbbus bridge doesn't have iicbus grandchild\n");
926bad0eccaSFrançois Tigeot 			goto err;
927bad0eccaSFrançois Tigeot 		}
928bad0eccaSFrançois Tigeot 
92919c468b4SFrançois Tigeot 		dev_priv->bbbus[pin] = iic_dev;
930bad0eccaSFrançois Tigeot 
93119c468b4SFrançois Tigeot 		dev_priv->gmbus_bridge[pin] = device_add_child(dev->dev,
93219c468b4SFrançois Tigeot 		    "intel_gmbus", pin);
93319c468b4SFrançois Tigeot 		if (dev_priv->gmbus_bridge[pin] == NULL) {
93419c468b4SFrançois Tigeot 			DRM_ERROR("gmbus bridge %d creation failed\n", pin);
935bad0eccaSFrançois Tigeot 			ret = ENXIO;
936bad0eccaSFrançois Tigeot 			goto err;
937bad0eccaSFrançois Tigeot 		}
93819c468b4SFrançois Tigeot 		device_quiet(dev_priv->gmbus_bridge[pin]);
93919c468b4SFrançois Tigeot 		ret = device_probe_and_attach(dev_priv->gmbus_bridge[pin]);
940bad0eccaSFrançois Tigeot 		if (ret != 0) {
94119c468b4SFrançois Tigeot 			DRM_ERROR("gmbus bridge %d attach failed, %d\n", pin,
942bad0eccaSFrançois Tigeot 			    ret);
943bad0eccaSFrançois Tigeot 			ret = ENXIO;
944bad0eccaSFrançois Tigeot 			goto err;
945bad0eccaSFrançois Tigeot 		}
946bad0eccaSFrançois Tigeot 
94719c468b4SFrançois Tigeot 		iic_dev = device_find_child(dev_priv->gmbus_bridge[pin],
948bad0eccaSFrançois Tigeot 		    "iicbus", -1);
949bad0eccaSFrançois Tigeot 		if (iic_dev == NULL) {
950bad0eccaSFrançois Tigeot 			DRM_ERROR("gmbus bridge doesn't have iicbus child\n");
951bad0eccaSFrançois Tigeot 			goto err;
952bad0eccaSFrançois Tigeot 		}
95319c468b4SFrançois Tigeot 		dev_priv->gmbus[pin] = iic_dev;
954bad0eccaSFrançois Tigeot 
9553f2f609dSFrançois Tigeot 		intel_i2c_reset(dev);
956bad0eccaSFrançois Tigeot 	}
957bad0eccaSFrançois Tigeot 
958bad0eccaSFrançois Tigeot 	return (0);
959bad0eccaSFrançois Tigeot 
960bad0eccaSFrançois Tigeot err:
96119c468b4SFrançois Tigeot 	intel_teardown_gmbus_m(dev, pin);
962bad0eccaSFrançois Tigeot 	return (ret);
963bad0eccaSFrançois Tigeot }
964bad0eccaSFrançois Tigeot 
965bad0eccaSFrançois Tigeot static void
966bad0eccaSFrançois Tigeot intel_teardown_gmbus_m(struct drm_device *dev, int m)
967bad0eccaSFrançois Tigeot {
968bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
969bad0eccaSFrançois Tigeot 
970bad0eccaSFrançois Tigeot 	dev_priv = dev->dev_private;
971bad0eccaSFrançois Tigeot 
972*175896dfSzrj 	kfree(dev_priv->gmbus);
973bad0eccaSFrançois Tigeot 	dev_priv->gmbus = NULL;
974*175896dfSzrj 	kfree(dev_priv->bbbus);
975bad0eccaSFrançois Tigeot 	dev_priv->bbbus = NULL;
976*175896dfSzrj 	kfree(dev_priv->gmbus_bridge);
977bad0eccaSFrançois Tigeot 	dev_priv->gmbus_bridge = NULL;
978*175896dfSzrj 	kfree(dev_priv->bbbus_bridge);
979bad0eccaSFrançois Tigeot 	dev_priv->bbbus_bridge = NULL;
98019df918dSFrançois Tigeot 	lockuninit(&dev_priv->gmbus_mutex);
981bad0eccaSFrançois Tigeot }
982bad0eccaSFrançois Tigeot 
983bad0eccaSFrançois Tigeot void
984bad0eccaSFrançois Tigeot intel_teardown_gmbus(struct drm_device *dev)
985bad0eccaSFrançois Tigeot {
986bad0eccaSFrançois Tigeot 
987bad0eccaSFrançois Tigeot 	get_mplock();
98819c468b4SFrançois Tigeot 	intel_teardown_gmbus_m(dev, GMBUS_NUM_PINS);
989bad0eccaSFrançois Tigeot 	rel_mplock();
990bad0eccaSFrançois Tigeot }
991