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