xref: /dflybsd-src/sys/dev/drm/i915/intel_i2c.c (revision 8e26cdf6811be99dd751b21dab89b96d9b33799a)
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 
72a2fdbec6SFrançois Tigeot struct gmbus_port {
73a2fdbec6SFrançois Tigeot 	const char *name;
74a2fdbec6SFrançois Tigeot 	int reg;
75a2fdbec6SFrançois Tigeot };
76a2fdbec6SFrançois Tigeot 
77a2fdbec6SFrançois Tigeot static const struct gmbus_port gmbus_ports[] = {
78a2fdbec6SFrançois Tigeot 	{ "ssc", GPIOB },
79a2fdbec6SFrançois Tigeot 	{ "vga", GPIOA },
80a2fdbec6SFrançois Tigeot 	{ "panel", GPIOC },
81a2fdbec6SFrançois Tigeot 	{ "dpc", GPIOD },
82a2fdbec6SFrançois Tigeot 	{ "dpb", GPIOE },
83a2fdbec6SFrançois Tigeot 	{ "dpd", GPIOF },
84a2fdbec6SFrançois Tigeot };
85bad0eccaSFrançois Tigeot 
86bad0eccaSFrançois Tigeot /* Intel GPIO access functions */
87bad0eccaSFrançois Tigeot 
88bad0eccaSFrançois Tigeot #define I2C_RISEFALL_TIME 10
89bad0eccaSFrançois Tigeot 
90a2fdbec6SFrançois Tigeot void
91a2fdbec6SFrançois Tigeot intel_i2c_reset(struct drm_device *dev)
92a2fdbec6SFrançois Tigeot {
93a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
94a2fdbec6SFrançois Tigeot 	I915_WRITE(dev_priv->gpio_mmio_base + GMBUS0, 0);
95a2fdbec6SFrançois Tigeot 	I915_WRITE(dev_priv->gpio_mmio_base + GMBUS4, 0);
96a2fdbec6SFrançois Tigeot }
97a2fdbec6SFrançois Tigeot 
98a2fdbec6SFrançois Tigeot static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
99bad0eccaSFrançois Tigeot {
100bad0eccaSFrançois Tigeot 	u32 val;
101bad0eccaSFrançois Tigeot 
102bad0eccaSFrançois Tigeot 	/* When using bit bashing for I2C, this bit needs to be set to 1 */
103bad0eccaSFrançois Tigeot 	if (!IS_PINEVIEW(dev_priv->dev))
104bad0eccaSFrançois Tigeot 		return;
105bad0eccaSFrançois Tigeot 
106bad0eccaSFrançois Tigeot 	val = I915_READ(DSPCLK_GATE_D);
107bad0eccaSFrançois Tigeot 	if (enable)
108bad0eccaSFrançois Tigeot 		val |= DPCUNIT_CLOCK_GATE_DISABLE;
109bad0eccaSFrançois Tigeot 	else
110bad0eccaSFrançois Tigeot 		val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
111bad0eccaSFrançois Tigeot 	I915_WRITE(DSPCLK_GATE_D, val);
112bad0eccaSFrançois Tigeot }
113bad0eccaSFrançois Tigeot 
114a2fdbec6SFrançois Tigeot static u32 get_reserved(device_t idev)
115a2fdbec6SFrançois Tigeot {
116a2fdbec6SFrançois Tigeot 	struct intel_iic_softc *sc = device_get_softc(idev);
117a2fdbec6SFrançois Tigeot 	struct drm_device *dev = sc->drm_dev;
118a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv;
119a2fdbec6SFrançois Tigeot 	u32 reserved = 0;
120a2fdbec6SFrançois Tigeot 
121a2fdbec6SFrançois Tigeot 	dev_priv = dev->dev_private;
122a2fdbec6SFrançois Tigeot 
123a2fdbec6SFrançois Tigeot 	/* On most chips, these bits must be preserved in software. */
124a2fdbec6SFrançois Tigeot 	if (!IS_I830(dev) && !IS_845G(dev))
125a2fdbec6SFrançois Tigeot 		reserved = I915_READ_NOTRACE(sc->reg) &
126a2fdbec6SFrançois Tigeot 					     (GPIO_DATA_PULLUP_DISABLE |
127a2fdbec6SFrançois Tigeot 					      GPIO_CLOCK_PULLUP_DISABLE);
128a2fdbec6SFrançois Tigeot 
129a2fdbec6SFrançois Tigeot 	return reserved;
130a2fdbec6SFrançois Tigeot }
131a2fdbec6SFrançois Tigeot 
132a2fdbec6SFrançois Tigeot static int get_clock(device_t idev)
133bad0eccaSFrançois Tigeot {
134bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
135bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
136bad0eccaSFrançois Tigeot 	u32 reserved;
137bad0eccaSFrançois Tigeot 
138bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
139a2fdbec6SFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
140bad0eccaSFrançois Tigeot 
141a2fdbec6SFrançois Tigeot 	reserved = get_reserved(idev);
142a2fdbec6SFrançois Tigeot 
143a2fdbec6SFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_CLOCK_DIR_MASK);
144a2fdbec6SFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved);
145a2fdbec6SFrançois Tigeot 	return ((I915_READ_NOTRACE(sc->reg) & GPIO_CLOCK_VAL_IN) != 0);
146bad0eccaSFrançois Tigeot }
147bad0eccaSFrançois Tigeot 
148a2fdbec6SFrançois Tigeot static int get_data(device_t idev)
149bad0eccaSFrançois Tigeot {
150a2fdbec6SFrançois Tigeot 	struct intel_iic_softc *sc;
151bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
152a2fdbec6SFrançois Tigeot 	u32 reserved;
153bad0eccaSFrançois Tigeot 
154a2fdbec6SFrançois Tigeot 	sc = device_get_softc(idev);
155a2fdbec6SFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
156a2fdbec6SFrançois Tigeot 
157a2fdbec6SFrançois Tigeot 	reserved = get_reserved(idev);
158a2fdbec6SFrançois Tigeot 
159a2fdbec6SFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_DATA_DIR_MASK);
160a2fdbec6SFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved);
161a2fdbec6SFrançois Tigeot 	return ((I915_READ_NOTRACE(sc->reg) & GPIO_DATA_VAL_IN) != 0);
162bad0eccaSFrançois Tigeot }
163bad0eccaSFrançois Tigeot 
164bad0eccaSFrançois Tigeot static int
165bad0eccaSFrançois Tigeot intel_iicbus_reset(device_t idev, u_char speed, u_char addr, u_char *oldaddr)
166bad0eccaSFrançois Tigeot {
167bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
168bad0eccaSFrançois Tigeot 	struct drm_device *dev;
169bad0eccaSFrançois Tigeot 
170bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
171bad0eccaSFrançois Tigeot 	dev = sc->drm_dev;
172bad0eccaSFrançois Tigeot 
1733f2f609dSFrançois Tigeot 	intel_i2c_reset(dev);
174bad0eccaSFrançois Tigeot 	return (0);
175bad0eccaSFrançois Tigeot }
176bad0eccaSFrançois Tigeot 
177a2fdbec6SFrançois Tigeot static void set_clock(device_t idev, int val)
178bad0eccaSFrançois Tigeot {
179bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
180bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
181bad0eccaSFrançois Tigeot 	u32 clock_bits, reserved;
182bad0eccaSFrançois Tigeot 
183bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
184bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
185bad0eccaSFrançois Tigeot 
186a2fdbec6SFrançois Tigeot 	reserved = get_reserved(idev);
187bad0eccaSFrançois Tigeot 	if (val)
188bad0eccaSFrançois Tigeot 		clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
189bad0eccaSFrançois Tigeot 	else
190bad0eccaSFrançois Tigeot 		clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
191bad0eccaSFrançois Tigeot 		    GPIO_CLOCK_VAL_MASK;
192bad0eccaSFrançois Tigeot 
193bad0eccaSFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved | clock_bits);
194bad0eccaSFrançois Tigeot 	POSTING_READ(sc->reg);
195bad0eccaSFrançois Tigeot }
196bad0eccaSFrançois Tigeot 
197a2fdbec6SFrançois Tigeot static void set_data(device_t idev, int val)
198bad0eccaSFrançois Tigeot {
199bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
200bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
201bad0eccaSFrançois Tigeot 	u32 reserved;
202a2fdbec6SFrançois Tigeot 	u32 data_bits;
203bad0eccaSFrançois Tigeot 
204bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
205bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
206bad0eccaSFrançois Tigeot 
207a2fdbec6SFrançois Tigeot 	reserved = get_reserved(idev);
208a2fdbec6SFrançois Tigeot 	if (val)
209a2fdbec6SFrançois Tigeot 		data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
210a2fdbec6SFrançois Tigeot 	else
211a2fdbec6SFrançois Tigeot 		data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
212a2fdbec6SFrançois Tigeot 		    GPIO_DATA_VAL_MASK;
213bad0eccaSFrançois Tigeot 
214a2fdbec6SFrançois Tigeot 	I915_WRITE_NOTRACE(sc->reg, reserved | data_bits);
215a2fdbec6SFrançois Tigeot 	POSTING_READ(sc->reg);
216bad0eccaSFrançois Tigeot }
217bad0eccaSFrançois Tigeot 
218a2fdbec6SFrançois Tigeot static const char *gpio_names[GMBUS_NUM_PORTS] = {
219a2fdbec6SFrançois Tigeot 	"ssc",
220a2fdbec6SFrançois Tigeot 	"vga",
221a2fdbec6SFrançois Tigeot 	"panel",
222a2fdbec6SFrançois Tigeot 	"dpc",
223a2fdbec6SFrançois Tigeot 	"dpb",
224a2fdbec6SFrançois Tigeot 	"dpd",
225a2fdbec6SFrançois Tigeot };
226a2fdbec6SFrançois Tigeot 
227bad0eccaSFrançois Tigeot static int
228a2fdbec6SFrançois Tigeot intel_gpio_setup(device_t idev)
229bad0eccaSFrançois Tigeot {
230a2fdbec6SFrançois Tigeot 	static const int map_pin_to_reg[] = {
231a2fdbec6SFrançois Tigeot 		0,
232a2fdbec6SFrançois Tigeot 		GPIOB,
233a2fdbec6SFrançois Tigeot 		GPIOA,
234a2fdbec6SFrançois Tigeot 		GPIOC,
235a2fdbec6SFrançois Tigeot 		GPIOD,
236a2fdbec6SFrançois Tigeot 		GPIOE,
237a2fdbec6SFrançois Tigeot 		GPIOF,
238a2fdbec6SFrançois Tigeot 		0
239a2fdbec6SFrançois Tigeot 	};
240a2fdbec6SFrançois Tigeot 
241bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
242bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
243a2fdbec6SFrançois Tigeot 	int pin;
244bad0eccaSFrançois Tigeot 
245bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
246a2fdbec6SFrançois Tigeot 	sc->drm_dev = device_get_softc(device_get_parent(idev));
247bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
248a2fdbec6SFrançois Tigeot 	pin = device_get_unit(idev);
249bad0eccaSFrançois Tigeot 
250a2fdbec6SFrançois Tigeot 	ksnprintf(sc->name, sizeof(sc->name), "i915 iicbb %s", gpio_names[pin]);
251a2fdbec6SFrançois Tigeot 	device_set_desc(idev, sc->name);
252bad0eccaSFrançois Tigeot 
253a2fdbec6SFrançois Tigeot 	sc->reg0 = (pin + 1) | GMBUS_RATE_100KHZ;
254a2fdbec6SFrançois Tigeot 	sc->reg = map_pin_to_reg[pin + 1];
255a2fdbec6SFrançois Tigeot 	if (HAS_PCH_SPLIT(dev_priv->dev))
256a2fdbec6SFrançois Tigeot 		sc->reg += PCH_GPIOA - GPIOA;
257a2fdbec6SFrançois Tigeot 
258a2fdbec6SFrançois Tigeot 	/* add generic bit-banging code */
259a2fdbec6SFrançois Tigeot 	sc->iic_dev = device_add_child(idev, "iicbb", -1);
260a2fdbec6SFrançois Tigeot 	if (sc->iic_dev == NULL)
261a2fdbec6SFrançois Tigeot 		return (ENXIO);
262a2fdbec6SFrançois Tigeot 	device_quiet(sc->iic_dev);
263a2fdbec6SFrançois Tigeot 	bus_generic_attach(idev);
264a2fdbec6SFrançois Tigeot 
265a2fdbec6SFrançois Tigeot 	return (0);
266bad0eccaSFrançois Tigeot }
267bad0eccaSFrançois Tigeot 
268bad0eccaSFrançois Tigeot static int
269a2fdbec6SFrançois Tigeot intel_i2c_quirk_xfer(device_t idev, struct iic_msg *msgs, int nmsgs)
270bad0eccaSFrançois Tigeot {
271a2fdbec6SFrançois Tigeot 	device_t bridge_dev;
272bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
273bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
274a2fdbec6SFrançois Tigeot 	int ret;
275a2fdbec6SFrançois Tigeot 	int i;
276a2fdbec6SFrançois Tigeot 
277a2fdbec6SFrançois Tigeot 	bridge_dev = device_get_parent(device_get_parent(idev));
278a2fdbec6SFrançois Tigeot 	sc = device_get_softc(bridge_dev);
279a2fdbec6SFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
280a2fdbec6SFrançois Tigeot 
281a2fdbec6SFrançois Tigeot 	intel_i2c_reset(sc->drm_dev);
282a2fdbec6SFrançois Tigeot 	intel_i2c_quirk_set(dev_priv, true);
283a2fdbec6SFrançois Tigeot 	IICBB_SETSDA(bridge_dev, 1);
284a2fdbec6SFrançois Tigeot 	IICBB_SETSCL(bridge_dev, 1);
285a2fdbec6SFrançois Tigeot 	DELAY(I2C_RISEFALL_TIME);
286a2fdbec6SFrançois Tigeot 
287a2fdbec6SFrançois Tigeot 	for (i = 0; i < nmsgs - 1; i++) {
288a2fdbec6SFrançois Tigeot 		/* force use of repeated start instead of default stop+start */
289a2fdbec6SFrançois Tigeot 		msgs[i].flags |= IIC_M_NOSTOP;
290a2fdbec6SFrançois Tigeot 	}
291a2fdbec6SFrançois Tigeot 	ret = iicbus_transfer(idev, msgs, nmsgs);
292a2fdbec6SFrançois Tigeot 	IICBB_SETSDA(bridge_dev, 1);
293a2fdbec6SFrançois Tigeot 	IICBB_SETSCL(bridge_dev, 1);
294a2fdbec6SFrançois Tigeot 	intel_i2c_quirk_set(dev_priv, false);
295a2fdbec6SFrançois Tigeot 
296a2fdbec6SFrançois Tigeot 	return (ret);
297a2fdbec6SFrançois Tigeot }
298a2fdbec6SFrançois Tigeot 
299a2fdbec6SFrançois Tigeot /*
300a2fdbec6SFrançois Tigeot  * gmbus on gen4 seems to be able to generate legacy interrupts even when in MSI
301a2fdbec6SFrançois Tigeot  * mode. This results in spurious interrupt warnings if the legacy irq no. is
302a2fdbec6SFrançois Tigeot  * shared with another device. The kernel then disables that interrupt source
303a2fdbec6SFrançois Tigeot  * and so prevents the other device from working properly.
304a2fdbec6SFrançois Tigeot  */
305a2fdbec6SFrançois Tigeot #define HAS_GMBUS_IRQ(dev) (INTEL_INFO(dev)->gen >= 5)
306a2fdbec6SFrançois Tigeot static int
307a2fdbec6SFrançois Tigeot gmbus_wait_hw_status(struct drm_i915_private *dev_priv,
308a2fdbec6SFrançois Tigeot 		     u32 gmbus2_status,
309a2fdbec6SFrançois Tigeot 		     u32 gmbus4_irq_en)
310a2fdbec6SFrançois Tigeot {
311a2fdbec6SFrançois Tigeot 	int i;
312a2fdbec6SFrançois Tigeot 	int reg_offset = dev_priv->gpio_mmio_base;
313a2fdbec6SFrançois Tigeot 	u32 gmbus2 = 0;
314a2fdbec6SFrançois Tigeot 	DEFINE_WAIT(wait);
315a2fdbec6SFrançois Tigeot 
316a2fdbec6SFrançois Tigeot 	if (!HAS_GMBUS_IRQ(dev_priv->dev))
317a2fdbec6SFrançois Tigeot 		gmbus4_irq_en = 0;
318a2fdbec6SFrançois Tigeot 
319a2fdbec6SFrançois Tigeot 	/* Important: The hw handles only the first bit, so set only one! Since
320a2fdbec6SFrançois Tigeot 	 * we also need to check for NAKs besides the hw ready/idle signal, we
321a2fdbec6SFrançois Tigeot 	 * need to wake up periodically and check that ourselves. */
322a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS4 + reg_offset, gmbus4_irq_en);
323a2fdbec6SFrançois Tigeot 
324*8e26cdf6SFrançois Tigeot 	for (i = 0; i < msecs_to_jiffies_timeout(50); i++) {
325a2fdbec6SFrançois Tigeot 		prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait,
326a2fdbec6SFrançois Tigeot 				TASK_UNINTERRUPTIBLE);
327a2fdbec6SFrançois Tigeot 
328a2fdbec6SFrançois Tigeot 		gmbus2 = I915_READ_NOTRACE(GMBUS2 + reg_offset);
329a2fdbec6SFrançois Tigeot 		if (gmbus2 & (GMBUS_SATOER | gmbus2_status))
330a2fdbec6SFrançois Tigeot 			break;
331a2fdbec6SFrançois Tigeot 
332a2fdbec6SFrançois Tigeot 		schedule_timeout(1);
333a2fdbec6SFrançois Tigeot 	}
334a2fdbec6SFrançois Tigeot 	finish_wait(&dev_priv->gmbus_wait_queue, &wait);
335a2fdbec6SFrançois Tigeot 
336a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS4 + reg_offset, 0);
337a2fdbec6SFrançois Tigeot 
338a2fdbec6SFrançois Tigeot 	if (gmbus2 & GMBUS_SATOER)
339a2fdbec6SFrançois Tigeot 		return -ENXIO;
340a2fdbec6SFrançois Tigeot 	if (gmbus2 & gmbus2_status)
341a2fdbec6SFrançois Tigeot 		return 0;
342a2fdbec6SFrançois Tigeot 	return -ETIMEDOUT;
343a2fdbec6SFrançois Tigeot }
344a2fdbec6SFrançois Tigeot 
345a2fdbec6SFrançois Tigeot static int
346a2fdbec6SFrançois Tigeot gmbus_wait_idle(struct drm_i915_private *dev_priv)
347a2fdbec6SFrançois Tigeot {
348a2fdbec6SFrançois Tigeot 	int ret;
349a2fdbec6SFrançois Tigeot 	int reg_offset = dev_priv->gpio_mmio_base;
350a2fdbec6SFrançois Tigeot 
351a2fdbec6SFrançois Tigeot #define C ((I915_READ_NOTRACE(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0)
352a2fdbec6SFrançois Tigeot 
353a2fdbec6SFrançois Tigeot 	if (!HAS_GMBUS_IRQ(dev_priv->dev))
354a2fdbec6SFrançois Tigeot 		return wait_for(C, 10);
355a2fdbec6SFrançois Tigeot 
356a2fdbec6SFrançois Tigeot 	/* Important: The hw handles only the first bit, so set only one! */
357a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS4 + reg_offset, GMBUS_IDLE_EN);
358a2fdbec6SFrançois Tigeot 
359*8e26cdf6SFrançois Tigeot 	ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
360*8e26cdf6SFrançois Tigeot 				 msecs_to_jiffies_timeout(10));
361a2fdbec6SFrançois Tigeot 
362a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS4 + reg_offset, 0);
363a2fdbec6SFrançois Tigeot 
364a2fdbec6SFrançois Tigeot 	if (ret)
365a2fdbec6SFrançois Tigeot 		return 0;
366a2fdbec6SFrançois Tigeot 	else
367a2fdbec6SFrançois Tigeot 		return -ETIMEDOUT;
368a2fdbec6SFrançois Tigeot #undef C
369a2fdbec6SFrançois Tigeot }
370a2fdbec6SFrançois Tigeot 
371a2fdbec6SFrançois Tigeot static int
372a2fdbec6SFrançois Tigeot gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
373a2fdbec6SFrançois Tigeot 		u32 gmbus1_index)
374a2fdbec6SFrançois Tigeot {
375a2fdbec6SFrançois Tigeot 	int reg_offset = dev_priv->gpio_mmio_base;
376a2fdbec6SFrançois Tigeot 	u16 len = msg->len;
377a2fdbec6SFrançois Tigeot 	u8 *buf = msg->buf;
378a2fdbec6SFrançois Tigeot 
379a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS1 + reg_offset,
380a2fdbec6SFrançois Tigeot 		   gmbus1_index |
381a2fdbec6SFrançois Tigeot 		   GMBUS_CYCLE_WAIT |
382a2fdbec6SFrançois Tigeot 		   (len << GMBUS_BYTE_COUNT_SHIFT) |
383a2fdbec6SFrançois Tigeot 		   (msg->slave << (GMBUS_SLAVE_ADDR_SHIFT - 1)) |
384a2fdbec6SFrançois Tigeot 		   GMBUS_SLAVE_READ | GMBUS_SW_RDY);
385a2fdbec6SFrançois Tigeot 	while (len) {
386a2fdbec6SFrançois Tigeot 		int ret;
387a2fdbec6SFrançois Tigeot 		u32 val, loop = 0;
388a2fdbec6SFrançois Tigeot 
389a2fdbec6SFrançois Tigeot 		ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
390a2fdbec6SFrançois Tigeot 					   GMBUS_HW_RDY_EN);
391a2fdbec6SFrançois Tigeot 		if (ret)
392a2fdbec6SFrançois Tigeot 			return ret;
393a2fdbec6SFrançois Tigeot 
394a2fdbec6SFrançois Tigeot 		val = I915_READ(GMBUS3 + reg_offset);
395a2fdbec6SFrançois Tigeot 		do {
396a2fdbec6SFrançois Tigeot 			*buf++ = val & 0xff;
397a2fdbec6SFrançois Tigeot 			val >>= 8;
398a2fdbec6SFrançois Tigeot 		} while (--len && ++loop < 4);
399a2fdbec6SFrançois Tigeot 	}
400a2fdbec6SFrançois Tigeot 
401a2fdbec6SFrançois Tigeot 	return 0;
402a2fdbec6SFrançois Tigeot }
403a2fdbec6SFrançois Tigeot 
404a2fdbec6SFrançois Tigeot static int
405a2fdbec6SFrançois Tigeot gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
406a2fdbec6SFrançois Tigeot {
407a2fdbec6SFrançois Tigeot 	int reg_offset = dev_priv->gpio_mmio_base;
408a2fdbec6SFrançois Tigeot 	u16 len = msg->len;
409a2fdbec6SFrançois Tigeot 	u8 *buf = msg->buf;
410bad0eccaSFrançois Tigeot 	u32 val, loop;
411bad0eccaSFrançois Tigeot 
412a2fdbec6SFrançois Tigeot 	val = loop = 0;
413a2fdbec6SFrançois Tigeot 	while (len && loop < 4) {
414a2fdbec6SFrançois Tigeot 		val |= *buf++ << (8 * loop++);
415a2fdbec6SFrançois Tigeot 		len -= 1;
416a2fdbec6SFrançois Tigeot 	}
417a2fdbec6SFrançois Tigeot 
418a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS3 + reg_offset, val);
419a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS1 + reg_offset,
420a2fdbec6SFrançois Tigeot 		   GMBUS_CYCLE_WAIT |
421a2fdbec6SFrançois Tigeot 		   (msg->len << GMBUS_BYTE_COUNT_SHIFT) |
422a2fdbec6SFrançois Tigeot 		   (msg->slave << (GMBUS_SLAVE_ADDR_SHIFT - 1)) |
423a2fdbec6SFrançois Tigeot 		   GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
424a2fdbec6SFrançois Tigeot 	while (len) {
425a2fdbec6SFrançois Tigeot 		int ret;
426a2fdbec6SFrançois Tigeot 
427a2fdbec6SFrançois Tigeot 		val = loop = 0;
428a2fdbec6SFrançois Tigeot 		do {
429a2fdbec6SFrançois Tigeot 			val |= *buf++ << (8 * loop);
430a2fdbec6SFrançois Tigeot 		} while (--len && ++loop < 4);
431a2fdbec6SFrançois Tigeot 
432a2fdbec6SFrançois Tigeot 		I915_WRITE(GMBUS3 + reg_offset, val);
433a2fdbec6SFrançois Tigeot 
434a2fdbec6SFrançois Tigeot 		ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
435a2fdbec6SFrançois Tigeot 					   GMBUS_HW_RDY_EN);
436a2fdbec6SFrançois Tigeot 		if (ret)
437a2fdbec6SFrançois Tigeot 			return ret;
438a2fdbec6SFrançois Tigeot 	}
439a2fdbec6SFrançois Tigeot 	return 0;
440a2fdbec6SFrançois Tigeot }
441a2fdbec6SFrançois Tigeot 
442a2fdbec6SFrançois Tigeot /*
443a2fdbec6SFrançois Tigeot  * The gmbus controller can combine a 1 or 2 byte write with a read that
444a2fdbec6SFrançois Tigeot  * immediately follows it by using an "INDEX" cycle.
445a2fdbec6SFrançois Tigeot  */
446a2fdbec6SFrançois Tigeot static bool
447a2fdbec6SFrançois Tigeot gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
448a2fdbec6SFrançois Tigeot {
449a2fdbec6SFrançois Tigeot 	return (i + 1 < num &&
450a2fdbec6SFrançois Tigeot 		!(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
451a2fdbec6SFrançois Tigeot 		(msgs[i + 1].flags & I2C_M_RD));
452a2fdbec6SFrançois Tigeot }
453a2fdbec6SFrançois Tigeot 
454a2fdbec6SFrançois Tigeot static int
455a2fdbec6SFrançois Tigeot gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
456a2fdbec6SFrançois Tigeot {
457a2fdbec6SFrançois Tigeot 	int reg_offset = dev_priv->gpio_mmio_base;
458a2fdbec6SFrançois Tigeot 	u32 gmbus1_index = 0;
459a2fdbec6SFrançois Tigeot 	u32 gmbus5 = 0;
460a2fdbec6SFrançois Tigeot 	int ret;
461a2fdbec6SFrançois Tigeot 
462a2fdbec6SFrançois Tigeot 	if (msgs[0].len == 2)
463a2fdbec6SFrançois Tigeot 		gmbus5 = GMBUS_2BYTE_INDEX_EN |
464a2fdbec6SFrançois Tigeot 			 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
465a2fdbec6SFrançois Tigeot 	if (msgs[0].len == 1)
466a2fdbec6SFrançois Tigeot 		gmbus1_index = GMBUS_CYCLE_INDEX |
467a2fdbec6SFrançois Tigeot 			       (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
468a2fdbec6SFrançois Tigeot 
469a2fdbec6SFrançois Tigeot 	/* GMBUS5 holds 16-bit index */
470a2fdbec6SFrançois Tigeot 	if (gmbus5)
471a2fdbec6SFrançois Tigeot 		I915_WRITE(GMBUS5 + reg_offset, gmbus5);
472a2fdbec6SFrançois Tigeot 
473a2fdbec6SFrançois Tigeot 	ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
474a2fdbec6SFrançois Tigeot 
475a2fdbec6SFrançois Tigeot 	/* Clear GMBUS5 after each index transfer */
476a2fdbec6SFrançois Tigeot 	if (gmbus5)
477a2fdbec6SFrançois Tigeot 		I915_WRITE(GMBUS5 + reg_offset, 0);
478a2fdbec6SFrançois Tigeot 
479a2fdbec6SFrançois Tigeot 	return ret;
480a2fdbec6SFrançois Tigeot }
481a2fdbec6SFrançois Tigeot 
482a2fdbec6SFrançois Tigeot static int
483a2fdbec6SFrançois Tigeot gmbus_xfer(struct device *adapter,
484a2fdbec6SFrançois Tigeot 	   struct i2c_msg *msgs,
485a2fdbec6SFrançois Tigeot 	   int num)
486a2fdbec6SFrançois Tigeot {
487a2fdbec6SFrançois Tigeot 	struct intel_iic_softc *sc;
488a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv;
489a2fdbec6SFrançois Tigeot 	int i, reg_offset, unit;
490a2fdbec6SFrançois Tigeot 	int ret = 0;
491a2fdbec6SFrançois Tigeot 
492a2fdbec6SFrançois Tigeot 	sc = device_get_softc(adapter);
493bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
494a2fdbec6SFrançois Tigeot 	unit = device_get_unit(adapter);
495bad0eccaSFrançois Tigeot 
496a2fdbec6SFrançois Tigeot 	mutex_lock(&dev_priv->gmbus_mutex);
497a2fdbec6SFrançois Tigeot 
498bad0eccaSFrançois Tigeot 	if (sc->force_bit_dev) {
499a2fdbec6SFrançois Tigeot 		ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num);
500bad0eccaSFrançois Tigeot 		goto out;
501bad0eccaSFrançois Tigeot 	}
502bad0eccaSFrançois Tigeot 
503bad0eccaSFrançois Tigeot 	reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0;
504bad0eccaSFrançois Tigeot 
505bad0eccaSFrançois Tigeot 	I915_WRITE(GMBUS0 + reg_offset, sc->reg0);
506bad0eccaSFrançois Tigeot 
507a2fdbec6SFrançois Tigeot 	for (i = 0; i < num; i++) {
508a2fdbec6SFrançois Tigeot 		if (gmbus_is_index_read(msgs, i, num)) {
509a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
510a2fdbec6SFrançois Tigeot 			i += 1;  /* set i to the index of the read xfer */
511a2fdbec6SFrançois Tigeot 		} else if (msgs[i].flags & I2C_M_RD) {
512a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
513bad0eccaSFrançois Tigeot 		} else {
514a2fdbec6SFrançois Tigeot 			ret = gmbus_xfer_write(dev_priv, &msgs[i]);
515a2fdbec6SFrançois Tigeot 		}
516bad0eccaSFrançois Tigeot 
517a2fdbec6SFrançois Tigeot 		if (ret == -ETIMEDOUT)
518bad0eccaSFrançois Tigeot 			goto timeout;
519a2fdbec6SFrançois Tigeot 		if (ret == -ENXIO)
520bad0eccaSFrançois Tigeot 			goto clear_err;
521bad0eccaSFrançois Tigeot 
522a2fdbec6SFrançois Tigeot 		ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE,
523a2fdbec6SFrançois Tigeot 					   GMBUS_HW_WAIT_EN);
524a2fdbec6SFrançois Tigeot 		if (ret == -ENXIO)
525bad0eccaSFrançois Tigeot 			goto clear_err;
526a2fdbec6SFrançois Tigeot 		if (ret)
527a2fdbec6SFrançois Tigeot 			goto timeout;
528bad0eccaSFrançois Tigeot 	}
529bad0eccaSFrançois Tigeot 
530a2fdbec6SFrançois Tigeot 	/* Generate a STOP condition on the bus. Note that gmbus can't generata
531a2fdbec6SFrançois Tigeot 	 * a STOP on the very first cycle. To simplify the code we
532a2fdbec6SFrançois Tigeot 	 * unconditionally generate the STOP condition with an additional gmbus
533a2fdbec6SFrançois Tigeot 	 * cycle. */
534a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
535a2fdbec6SFrançois Tigeot 
536bad0eccaSFrançois Tigeot 	/* Mark the GMBUS interface as disabled after waiting for idle.
537bad0eccaSFrançois Tigeot 	 * We will re-enable it at the start of the next xfer,
538bad0eccaSFrançois Tigeot 	 * till then let it sleep.
539bad0eccaSFrançois Tigeot 	 */
540a2fdbec6SFrançois Tigeot 	if (gmbus_wait_idle(dev_priv)) {
541a2fdbec6SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
542a2fdbec6SFrançois Tigeot 			 sc->name);
543a2fdbec6SFrançois Tigeot 		ret = -ETIMEDOUT;
544a2fdbec6SFrançois Tigeot 	}
545bad0eccaSFrançois Tigeot 	I915_WRITE(GMBUS0 + reg_offset, 0);
546a2fdbec6SFrançois Tigeot 	ret = ret ?: i;
547a2fdbec6SFrançois Tigeot 	goto timeout;	/* XXX: should be out */
548bad0eccaSFrançois Tigeot 
549bad0eccaSFrançois Tigeot clear_err:
550a2fdbec6SFrançois Tigeot 	/*
551a2fdbec6SFrançois Tigeot 	 * Wait for bus to IDLE before clearing NAK.
552a2fdbec6SFrançois Tigeot 	 * If we clear the NAK while bus is still active, then it will stay
553a2fdbec6SFrançois Tigeot 	 * active and the next transaction may fail.
554a2fdbec6SFrançois Tigeot 	 *
555a2fdbec6SFrançois Tigeot 	 * If no ACK is received during the address phase of a transaction, the
556a2fdbec6SFrançois Tigeot 	 * adapter must report -ENXIO. It is not clear what to return if no ACK
557a2fdbec6SFrançois Tigeot 	 * is received at other times. But we have to be careful to not return
558a2fdbec6SFrançois Tigeot 	 * spurious -ENXIO because that will prevent i2c and drm edid functions
559a2fdbec6SFrançois Tigeot 	 * from retrying. So return -ENXIO only when gmbus properly quiescents -
560a2fdbec6SFrançois Tigeot 	 * timing out seems to happen when there _is_ a ddc chip present, but
561a2fdbec6SFrançois Tigeot 	 * it's slow responding and only answers on the 2nd retry.
562a2fdbec6SFrançois Tigeot 	 */
563a2fdbec6SFrançois Tigeot 	ret = -ENXIO;
564a2fdbec6SFrançois Tigeot 	if (gmbus_wait_idle(dev_priv)) {
565a2fdbec6SFrançois Tigeot 		DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
566a2fdbec6SFrançois Tigeot 			      sc->name);
567a2fdbec6SFrançois Tigeot 		ret = -ETIMEDOUT;
568a2fdbec6SFrançois Tigeot 	}
569a2fdbec6SFrançois Tigeot 
570bad0eccaSFrançois Tigeot 	/* Toggle the Software Clear Interrupt bit. This has the effect
571bad0eccaSFrançois Tigeot 	 * of resetting the GMBUS controller and so clearing the
572bad0eccaSFrançois Tigeot 	 * BUS_ERROR raised by the slave's NAK.
573bad0eccaSFrançois Tigeot 	 */
574bad0eccaSFrançois Tigeot 	I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
575bad0eccaSFrançois Tigeot 	I915_WRITE(GMBUS1 + reg_offset, 0);
576bad0eccaSFrançois Tigeot 	I915_WRITE(GMBUS0 + reg_offset, 0);
577bad0eccaSFrançois Tigeot 
578a2fdbec6SFrançois Tigeot 	DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
579a2fdbec6SFrançois Tigeot 			 sc->name, msgs[i].slave,
580a2fdbec6SFrançois Tigeot 			 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
581bad0eccaSFrançois Tigeot 
582bad0eccaSFrançois Tigeot 	goto out;
583a2fdbec6SFrançois Tigeot 
584a2fdbec6SFrançois Tigeot timeout:
585a2fdbec6SFrançois Tigeot 	DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
586a2fdbec6SFrançois Tigeot 		 sc->name, sc->reg0 & 0xff);
587a2fdbec6SFrançois Tigeot 	I915_WRITE(GMBUS0 + reg_offset, 0);
588a2fdbec6SFrançois Tigeot 
589a2fdbec6SFrançois Tigeot 	/* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
590a2fdbec6SFrançois Tigeot 	sc->force_bit_dev = true;
591a2fdbec6SFrançois Tigeot 	ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num);
592a2fdbec6SFrançois Tigeot 
593a2fdbec6SFrançois Tigeot out:
594a2fdbec6SFrançois Tigeot 	mutex_unlock(&dev_priv->gmbus_mutex);
595a2fdbec6SFrançois Tigeot 	return ret;
596bad0eccaSFrançois Tigeot }
597bad0eccaSFrançois Tigeot 
59819df918dSFrançois Tigeot struct device *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
59919df918dSFrançois Tigeot 					    unsigned port)
60019df918dSFrançois Tigeot {
60119df918dSFrançois Tigeot 	WARN_ON(!intel_gmbus_is_port_valid(port));
60219df918dSFrançois Tigeot 	/* -1 to map pin pair to gmbus index */
60319df918dSFrançois Tigeot 	return (intel_gmbus_is_port_valid(port)) ?
60453f7629eSMarkus Pfeiffer 		dev_priv->gmbus[port-1] : NULL;
60519df918dSFrançois Tigeot }
60619df918dSFrançois Tigeot 
607bad0eccaSFrançois Tigeot void
608bad0eccaSFrançois Tigeot intel_gmbus_set_speed(device_t idev, int speed)
609bad0eccaSFrançois Tigeot {
610bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
611bad0eccaSFrançois Tigeot 
612bad0eccaSFrançois Tigeot 	sc = device_get_softc(device_get_parent(idev));
613bad0eccaSFrançois Tigeot 
614bad0eccaSFrançois Tigeot 	sc->reg0 = (sc->reg0 & ~(0x3 << 8)) | speed;
615bad0eccaSFrançois Tigeot }
616bad0eccaSFrançois Tigeot 
617bad0eccaSFrançois Tigeot void
618bad0eccaSFrançois Tigeot intel_gmbus_force_bit(device_t idev, bool force_bit)
619bad0eccaSFrançois Tigeot {
620bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
621bad0eccaSFrançois Tigeot 
622bad0eccaSFrançois Tigeot 	sc = device_get_softc(device_get_parent(idev));
623a2fdbec6SFrançois Tigeot 	sc->force_bit_dev += force_bit ? 1 : -1;
624a2fdbec6SFrançois Tigeot 	DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
625a2fdbec6SFrançois Tigeot 		      force_bit ? "en" : "dis", sc->name,
626a2fdbec6SFrançois Tigeot 		      sc->force_bit_dev);
627bad0eccaSFrançois Tigeot }
628bad0eccaSFrançois Tigeot 
629bad0eccaSFrançois Tigeot static int
630bad0eccaSFrançois Tigeot intel_gmbus_probe(device_t dev)
631bad0eccaSFrançois Tigeot {
632bad0eccaSFrançois Tigeot 
633bad0eccaSFrançois Tigeot 	return (BUS_PROBE_SPECIFIC);
634bad0eccaSFrançois Tigeot }
635bad0eccaSFrançois Tigeot 
636bad0eccaSFrançois Tigeot static int
637bad0eccaSFrançois Tigeot intel_gmbus_attach(device_t idev)
638bad0eccaSFrançois Tigeot {
639bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
640bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
641bad0eccaSFrançois Tigeot 	int pin;
642bad0eccaSFrançois Tigeot 
643bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
644bad0eccaSFrançois Tigeot 	sc->drm_dev = device_get_softc(device_get_parent(idev));
645bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
646bad0eccaSFrançois Tigeot 	pin = device_get_unit(idev);
647bad0eccaSFrançois Tigeot 
648bad0eccaSFrançois Tigeot 	ksnprintf(sc->name, sizeof(sc->name), "gmbus bus %s", gpio_names[pin]);
649bad0eccaSFrançois Tigeot 	device_set_desc(idev, sc->name);
650bad0eccaSFrançois Tigeot 
651bad0eccaSFrançois Tigeot 	/* By default use a conservative clock rate */
65253f7629eSMarkus Pfeiffer 	sc->reg0 = (pin + 1) | GMBUS_RATE_100KHZ;
653bad0eccaSFrançois Tigeot 
654bad0eccaSFrançois Tigeot 	/* XXX force bit banging until GMBUS is fully debugged */
655bad0eccaSFrançois Tigeot 	if (IS_GEN2(sc->drm_dev)) {
656bad0eccaSFrançois Tigeot 		sc->force_bit_dev = true;
657bad0eccaSFrançois Tigeot 	}
658bad0eccaSFrançois Tigeot 
659bad0eccaSFrançois Tigeot 	/* add bus interface device */
660bad0eccaSFrançois Tigeot 	sc->iic_dev = device_add_child(idev, "iicbus", -1);
661bad0eccaSFrançois Tigeot 	if (sc->iic_dev == NULL)
662bad0eccaSFrançois Tigeot 		return (ENXIO);
663bad0eccaSFrançois Tigeot 	device_quiet(sc->iic_dev);
664bad0eccaSFrançois Tigeot 	bus_generic_attach(idev);
665bad0eccaSFrançois Tigeot 
666bad0eccaSFrançois Tigeot 	return (0);
667bad0eccaSFrançois Tigeot }
668bad0eccaSFrançois Tigeot 
669bad0eccaSFrançois Tigeot static int
670bad0eccaSFrançois Tigeot intel_gmbus_detach(device_t idev)
671bad0eccaSFrançois Tigeot {
672bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
673bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
674bad0eccaSFrançois Tigeot 	device_t child;
675bad0eccaSFrançois Tigeot 	int u;
676bad0eccaSFrançois Tigeot 
677bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
678bad0eccaSFrançois Tigeot 	u = device_get_unit(idev);
679bad0eccaSFrançois Tigeot 	dev_priv = sc->drm_dev->dev_private;
680bad0eccaSFrançois Tigeot 
681bad0eccaSFrançois Tigeot 	child = sc->iic_dev;
682bad0eccaSFrançois Tigeot 	bus_generic_detach(idev);
683bad0eccaSFrançois Tigeot 	if (child != NULL)
684bad0eccaSFrançois Tigeot 		device_delete_child(idev, child);
685bad0eccaSFrançois Tigeot 
686bad0eccaSFrançois Tigeot 	return (0);
687bad0eccaSFrançois Tigeot }
688bad0eccaSFrançois Tigeot 
689bad0eccaSFrançois Tigeot static int
690bad0eccaSFrançois Tigeot intel_iicbb_probe(device_t dev)
691bad0eccaSFrançois Tigeot {
692bad0eccaSFrançois Tigeot 
693bad0eccaSFrançois Tigeot 	return (BUS_PROBE_DEFAULT);
694bad0eccaSFrançois Tigeot }
695bad0eccaSFrançois Tigeot 
696bad0eccaSFrançois Tigeot static int
697bad0eccaSFrançois Tigeot intel_iicbb_detach(device_t idev)
698bad0eccaSFrançois Tigeot {
699bad0eccaSFrançois Tigeot 	struct intel_iic_softc *sc;
700bad0eccaSFrançois Tigeot 	device_t child;
701bad0eccaSFrançois Tigeot 
702bad0eccaSFrançois Tigeot 	sc = device_get_softc(idev);
703bad0eccaSFrançois Tigeot 	child = sc->iic_dev;
704bad0eccaSFrançois Tigeot 	bus_generic_detach(idev);
705bad0eccaSFrançois Tigeot 	if (child)
706bad0eccaSFrançois Tigeot 		device_delete_child(idev, child);
707bad0eccaSFrançois Tigeot 	return (0);
708bad0eccaSFrançois Tigeot }
709bad0eccaSFrançois Tigeot 
710bad0eccaSFrançois Tigeot static device_method_t intel_gmbus_methods[] = {
711bad0eccaSFrançois Tigeot 	DEVMETHOD(device_probe,		intel_gmbus_probe),
712bad0eccaSFrançois Tigeot 	DEVMETHOD(device_attach,	intel_gmbus_attach),
713bad0eccaSFrançois Tigeot 	DEVMETHOD(device_detach,	intel_gmbus_detach),
714bad0eccaSFrançois Tigeot 	DEVMETHOD(iicbus_reset,		intel_iicbus_reset),
715a2fdbec6SFrançois Tigeot 	DEVMETHOD(iicbus_transfer,	gmbus_xfer),
716bad0eccaSFrançois Tigeot 	DEVMETHOD_END
717bad0eccaSFrançois Tigeot };
718bad0eccaSFrançois Tigeot static driver_t intel_gmbus_driver = {
719bad0eccaSFrançois Tigeot 	"intel_gmbus",
720bad0eccaSFrançois Tigeot 	intel_gmbus_methods,
721bad0eccaSFrançois Tigeot 	sizeof(struct intel_iic_softc)
722bad0eccaSFrançois Tigeot };
723bad0eccaSFrançois Tigeot static devclass_t intel_gmbus_devclass;
724bad0eccaSFrançois Tigeot DRIVER_MODULE_ORDERED(intel_gmbus, drm, intel_gmbus_driver,
725bad0eccaSFrançois Tigeot     intel_gmbus_devclass, 0, 0, SI_ORDER_FIRST);
7263a25be87SSascha Wildner DRIVER_MODULE(iicbus, intel_gmbus, iicbus_driver, iicbus_devclass, NULL, NULL);
727bad0eccaSFrançois Tigeot 
728bad0eccaSFrançois Tigeot static device_method_t intel_iicbb_methods[] =	{
729bad0eccaSFrançois Tigeot 	DEVMETHOD(device_probe,		intel_iicbb_probe),
730a2fdbec6SFrançois Tigeot 	DEVMETHOD(device_attach,	intel_gpio_setup),
731bad0eccaSFrançois Tigeot 	DEVMETHOD(device_detach,	intel_iicbb_detach),
732bad0eccaSFrançois Tigeot 
733bad0eccaSFrançois Tigeot 	DEVMETHOD(bus_add_child,	bus_generic_add_child),
734bad0eccaSFrançois Tigeot 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
735bad0eccaSFrançois Tigeot 
736bad0eccaSFrançois Tigeot 	DEVMETHOD(iicbb_callback,	iicbus_null_callback),
737bad0eccaSFrançois Tigeot 	DEVMETHOD(iicbb_reset,		intel_iicbus_reset),
738a2fdbec6SFrançois Tigeot 	DEVMETHOD(iicbb_setsda,		set_data),
739a2fdbec6SFrançois Tigeot 	DEVMETHOD(iicbb_setscl,		set_clock),
740a2fdbec6SFrançois Tigeot 	DEVMETHOD(iicbb_getsda,		get_data),
741a2fdbec6SFrançois Tigeot 	DEVMETHOD(iicbb_getscl,		get_clock),
742bad0eccaSFrançois Tigeot 	DEVMETHOD_END
743bad0eccaSFrançois Tigeot };
744bad0eccaSFrançois Tigeot static driver_t intel_iicbb_driver = {
745bad0eccaSFrançois Tigeot 	"intel_iicbb",
746bad0eccaSFrançois Tigeot 	intel_iicbb_methods,
747bad0eccaSFrançois Tigeot 	sizeof(struct intel_iic_softc)
748bad0eccaSFrançois Tigeot };
749bad0eccaSFrançois Tigeot static devclass_t intel_iicbb_devclass;
750bad0eccaSFrançois Tigeot DRIVER_MODULE_ORDERED(intel_iicbb, drm, intel_iicbb_driver,
751bad0eccaSFrançois Tigeot     intel_iicbb_devclass, 0, 0, SI_ORDER_FIRST);
7523a25be87SSascha Wildner DRIVER_MODULE(iicbb, intel_iicbb, iicbb_driver, iicbb_devclass, NULL, NULL);
753bad0eccaSFrançois Tigeot 
754a2fdbec6SFrançois Tigeot static void intel_teardown_gmbus_m(struct drm_device *dev, int m);
755a2fdbec6SFrançois Tigeot 
756bad0eccaSFrançois Tigeot int
757bad0eccaSFrançois Tigeot intel_setup_gmbus(struct drm_device *dev)
758bad0eccaSFrançois Tigeot {
759a2fdbec6SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
760bad0eccaSFrançois Tigeot 	device_t iic_dev;
761bad0eccaSFrançois Tigeot 	int i, ret;
762bad0eccaSFrançois Tigeot 
763*8e26cdf6SFrançois Tigeot 	if (HAS_PCH_NOP(dev))
764*8e26cdf6SFrançois Tigeot 		return 0;
765*8e26cdf6SFrançois Tigeot 	else if (HAS_PCH_SPLIT(dev))
766a2fdbec6SFrançois Tigeot 		dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
767a2fdbec6SFrançois Tigeot 	else if (IS_VALLEYVIEW(dev))
768a2fdbec6SFrançois Tigeot 		dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
769a2fdbec6SFrançois Tigeot 	else
770a2fdbec6SFrançois Tigeot 		dev_priv->gpio_mmio_base = 0;
771a2fdbec6SFrançois Tigeot 
77219df918dSFrançois Tigeot 	lockinit(&dev_priv->gmbus_mutex, "gmbus", 0, LK_CANRECURSE);
773a2fdbec6SFrançois Tigeot 	init_waitqueue_head(&dev_priv->gmbus_wait_queue);
774a2fdbec6SFrançois Tigeot 
775bad0eccaSFrançois Tigeot 	dev_priv->gmbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
7765a3b77d5SFrançois Tigeot 	    M_DRM, M_WAITOK | M_ZERO);
777bad0eccaSFrançois Tigeot 	dev_priv->bbbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
7785a3b77d5SFrançois Tigeot 	    M_DRM, M_WAITOK | M_ZERO);
779bad0eccaSFrançois Tigeot 	dev_priv->gmbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
7805a3b77d5SFrançois Tigeot 	    M_DRM, M_WAITOK | M_ZERO);
781bad0eccaSFrançois Tigeot 	dev_priv->bbbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
7825a3b77d5SFrançois Tigeot 	    M_DRM, M_WAITOK | M_ZERO);
783bad0eccaSFrançois Tigeot 
784bad0eccaSFrançois Tigeot 	for (i = 0; i < GMBUS_NUM_PORTS; i++) {
785bad0eccaSFrançois Tigeot 		/*
786bad0eccaSFrançois Tigeot 		 * Initialized bbbus_bridge before gmbus_bridge, since
787bad0eccaSFrançois Tigeot 		 * gmbus may decide to force quirk transfer in the
788bad0eccaSFrançois Tigeot 		 * attachment code.
789bad0eccaSFrançois Tigeot 		 */
790bad0eccaSFrançois Tigeot 		dev_priv->bbbus_bridge[i] = device_add_child(dev->dev,
791bad0eccaSFrançois Tigeot 		    "intel_iicbb", i);
792bad0eccaSFrançois Tigeot 		if (dev_priv->bbbus_bridge[i] == NULL) {
793bad0eccaSFrançois Tigeot 			DRM_ERROR("bbbus bridge %d creation failed\n", i);
794bad0eccaSFrançois Tigeot 			ret = ENXIO;
795bad0eccaSFrançois Tigeot 			goto err;
796bad0eccaSFrançois Tigeot 		}
797bad0eccaSFrançois Tigeot 		device_quiet(dev_priv->bbbus_bridge[i]);
798bad0eccaSFrançois Tigeot 		ret = device_probe_and_attach(dev_priv->bbbus_bridge[i]);
799bad0eccaSFrançois Tigeot 		if (ret != 0) {
800bad0eccaSFrançois Tigeot 			DRM_ERROR("bbbus bridge %d attach failed, %d\n", i,
801bad0eccaSFrançois Tigeot 			    ret);
802bad0eccaSFrançois Tigeot 			goto err;
803bad0eccaSFrançois Tigeot 		}
804bad0eccaSFrançois Tigeot 
805bad0eccaSFrançois Tigeot 		iic_dev = device_find_child(dev_priv->bbbus_bridge[i], "iicbb",
806bad0eccaSFrançois Tigeot 		    -1);
807bad0eccaSFrançois Tigeot 		if (iic_dev == NULL) {
808bad0eccaSFrançois Tigeot 			DRM_ERROR("bbbus bridge doesn't have iicbb child\n");
809bad0eccaSFrançois Tigeot 			goto err;
810bad0eccaSFrançois Tigeot 		}
811bad0eccaSFrançois Tigeot 		iic_dev = device_find_child(iic_dev, "iicbus", -1);
812bad0eccaSFrançois Tigeot 		if (iic_dev == NULL) {
813bad0eccaSFrançois Tigeot 			DRM_ERROR(
814bad0eccaSFrançois Tigeot 		"bbbus bridge doesn't have iicbus grandchild\n");
815bad0eccaSFrançois Tigeot 			goto err;
816bad0eccaSFrançois Tigeot 		}
817bad0eccaSFrançois Tigeot 
818bad0eccaSFrançois Tigeot 		dev_priv->bbbus[i] = iic_dev;
819bad0eccaSFrançois Tigeot 
820bad0eccaSFrançois Tigeot 		dev_priv->gmbus_bridge[i] = device_add_child(dev->dev,
821bad0eccaSFrançois Tigeot 		    "intel_gmbus", i);
822bad0eccaSFrançois Tigeot 		if (dev_priv->gmbus_bridge[i] == NULL) {
823bad0eccaSFrançois Tigeot 			DRM_ERROR("gmbus bridge %d creation failed\n", i);
824bad0eccaSFrançois Tigeot 			ret = ENXIO;
825bad0eccaSFrançois Tigeot 			goto err;
826bad0eccaSFrançois Tigeot 		}
827bad0eccaSFrançois Tigeot 		device_quiet(dev_priv->gmbus_bridge[i]);
828bad0eccaSFrançois Tigeot 		ret = device_probe_and_attach(dev_priv->gmbus_bridge[i]);
829bad0eccaSFrançois Tigeot 		if (ret != 0) {
830bad0eccaSFrançois Tigeot 			DRM_ERROR("gmbus bridge %d attach failed, %d\n", i,
831bad0eccaSFrançois Tigeot 			    ret);
832bad0eccaSFrançois Tigeot 			ret = ENXIO;
833bad0eccaSFrançois Tigeot 			goto err;
834bad0eccaSFrançois Tigeot 		}
835bad0eccaSFrançois Tigeot 
836bad0eccaSFrançois Tigeot 		iic_dev = device_find_child(dev_priv->gmbus_bridge[i],
837bad0eccaSFrançois Tigeot 		    "iicbus", -1);
838bad0eccaSFrançois Tigeot 		if (iic_dev == NULL) {
839bad0eccaSFrançois Tigeot 			DRM_ERROR("gmbus bridge doesn't have iicbus child\n");
840bad0eccaSFrançois Tigeot 			goto err;
841bad0eccaSFrançois Tigeot 		}
842bad0eccaSFrançois Tigeot 		dev_priv->gmbus[i] = iic_dev;
843bad0eccaSFrançois Tigeot 
8443f2f609dSFrançois Tigeot 		intel_i2c_reset(dev);
845bad0eccaSFrançois Tigeot 	}
846bad0eccaSFrançois Tigeot 
847bad0eccaSFrançois Tigeot 	return (0);
848bad0eccaSFrançois Tigeot 
849bad0eccaSFrançois Tigeot err:
850bad0eccaSFrançois Tigeot 	intel_teardown_gmbus_m(dev, i);
851bad0eccaSFrançois Tigeot 	return (ret);
852bad0eccaSFrançois Tigeot }
853bad0eccaSFrançois Tigeot 
854bad0eccaSFrançois Tigeot static void
855bad0eccaSFrançois Tigeot intel_teardown_gmbus_m(struct drm_device *dev, int m)
856bad0eccaSFrançois Tigeot {
857bad0eccaSFrançois Tigeot 	struct drm_i915_private *dev_priv;
858bad0eccaSFrançois Tigeot 
859bad0eccaSFrançois Tigeot 	dev_priv = dev->dev_private;
860bad0eccaSFrançois Tigeot 
8615a3b77d5SFrançois Tigeot 	drm_free(dev_priv->gmbus, M_DRM);
862bad0eccaSFrançois Tigeot 	dev_priv->gmbus = NULL;
8635a3b77d5SFrançois Tigeot 	drm_free(dev_priv->bbbus, M_DRM);
864bad0eccaSFrançois Tigeot 	dev_priv->bbbus = NULL;
8655a3b77d5SFrançois Tigeot 	drm_free(dev_priv->gmbus_bridge, M_DRM);
866bad0eccaSFrançois Tigeot 	dev_priv->gmbus_bridge = NULL;
8675a3b77d5SFrançois Tigeot 	drm_free(dev_priv->bbbus_bridge, M_DRM);
868bad0eccaSFrançois Tigeot 	dev_priv->bbbus_bridge = NULL;
86919df918dSFrançois Tigeot 	lockuninit(&dev_priv->gmbus_mutex);
870bad0eccaSFrançois Tigeot }
871bad0eccaSFrançois Tigeot 
872bad0eccaSFrançois Tigeot void
873bad0eccaSFrançois Tigeot intel_teardown_gmbus(struct drm_device *dev)
874bad0eccaSFrançois Tigeot {
875bad0eccaSFrançois Tigeot 
876bad0eccaSFrançois Tigeot 	get_mplock();
877bad0eccaSFrançois Tigeot 	intel_teardown_gmbus_m(dev, GMBUS_NUM_PORTS);
878bad0eccaSFrançois Tigeot 	rel_mplock();
879bad0eccaSFrançois Tigeot }
880