2 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2008,2010 Intel Corporation
4 * Jesse Barnes <jesse.barnes@intel.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
26 * Eric Anholt <eric@anholt.net>
27 * Chris Wilson <chris@chris-wilson.co.uk>
29 * Copyright (c) 2011 The FreeBSD Foundation
30 * All rights reserved.
32 * This software was developed by Konstantin Belousov under sponsorship from
33 * the FreeBSD Foundation.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * $FreeBSD: head/sys/dev/drm2/i915/intel_iic.c 249041 2013-04-03 08:27:35Z dumbbell $
59 #include <sys/mplock2.h>
61 #include <dev/drm/drmP.h>
62 #include <dev/drm/drm.h>
65 #include "intel_drv.h"
66 #include <bus/iicbus/iic.h>
67 #include <bus/iicbus/iiconf.h>
68 #include <bus/iicbus/iicbus.h>
69 #include "iicbus_if.h"
72 static int intel_iic_quirk_xfer(device_t idev, struct iic_msg *msgs, int nmsgs);
73 static void intel_teardown_gmbus_m(struct drm_device *dev, int m);
75 /* Intel GPIO access functions */
77 #define I2C_RISEFALL_TIME 10
79 struct intel_iic_softc {
80 struct drm_device *drm_dev;
89 intel_iic_quirk_set(struct drm_i915_private *dev_priv, bool enable)
93 /* When using bit bashing for I2C, this bit needs to be set to 1 */
94 if (!IS_PINEVIEW(dev_priv->dev))
97 val = I915_READ(DSPCLK_GATE_D);
99 val |= DPCUNIT_CLOCK_GATE_DISABLE;
101 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
102 I915_WRITE(DSPCLK_GATE_D, val);
106 intel_iic_get_reserved(device_t idev)
108 struct intel_iic_softc *sc;
109 struct drm_device *dev;
110 struct drm_i915_private *dev_priv;
113 sc = device_get_softc(idev);
115 dev_priv = dev->dev_private;
117 if (!IS_I830(dev) && !IS_845G(dev)) {
118 reserved = I915_READ_NOTRACE(sc->reg) &
119 (GPIO_DATA_PULLUP_DISABLE | GPIO_CLOCK_PULLUP_DISABLE);
128 intel_iic_reset(struct drm_device *dev)
130 struct drm_i915_private *dev_priv;
132 dev_priv = dev->dev_private;
133 if (HAS_PCH_SPLIT(dev))
134 I915_WRITE(PCH_GMBUS0, 0);
136 I915_WRITE(GMBUS0, 0);
140 intel_iicbus_reset(device_t idev, u_char speed, u_char addr, u_char *oldaddr)
142 struct intel_iic_softc *sc;
143 struct drm_device *dev;
145 sc = device_get_softc(idev);
148 intel_iic_reset(dev);
153 intel_iicbb_setsda(device_t idev, int val)
155 struct intel_iic_softc *sc;
156 struct drm_i915_private *dev_priv;
160 sc = device_get_softc(idev);
161 dev_priv = sc->drm_dev->dev_private;
163 reserved = intel_iic_get_reserved(idev);
165 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
167 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
170 I915_WRITE_NOTRACE(sc->reg, reserved | data_bits);
171 POSTING_READ(sc->reg);
175 intel_iicbb_setscl(device_t idev, int val)
177 struct intel_iic_softc *sc;
178 struct drm_i915_private *dev_priv;
179 u32 clock_bits, reserved;
181 sc = device_get_softc(idev);
182 dev_priv = sc->drm_dev->dev_private;
184 reserved = intel_iic_get_reserved(idev);
186 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
188 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
191 I915_WRITE_NOTRACE(sc->reg, reserved | clock_bits);
192 POSTING_READ(sc->reg);
196 intel_iicbb_getsda(device_t idev)
198 struct intel_iic_softc *sc;
199 struct drm_i915_private *dev_priv;
202 sc = device_get_softc(idev);
203 dev_priv = sc->drm_dev->dev_private;
205 reserved = intel_iic_get_reserved(idev);
207 I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_DATA_DIR_MASK);
208 I915_WRITE_NOTRACE(sc->reg, reserved);
209 return ((I915_READ_NOTRACE(sc->reg) & GPIO_DATA_VAL_IN) != 0);
213 intel_iicbb_getscl(device_t idev)
215 struct intel_iic_softc *sc;
216 struct drm_i915_private *dev_priv;
219 sc = device_get_softc(idev);
220 dev_priv = sc->drm_dev->dev_private;
222 reserved = intel_iic_get_reserved(idev);
224 I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_CLOCK_DIR_MASK);
225 I915_WRITE_NOTRACE(sc->reg, reserved);
226 return ((I915_READ_NOTRACE(sc->reg) & GPIO_CLOCK_VAL_IN) != 0);
230 intel_gmbus_transfer(device_t idev, struct iic_msg *msgs, uint32_t nmsgs)
232 struct intel_iic_softc *sc;
233 struct drm_i915_private *dev_priv;
235 int error, i, reg_offset, unit;
239 sc = device_get_softc(idev);
240 dev_priv = sc->drm_dev->dev_private;
241 unit = device_get_unit(idev);
243 lockmgr(&dev_priv->gmbus_lock, LK_EXCLUSIVE);
244 if (sc->force_bit_dev) {
245 error = intel_iic_quirk_xfer(dev_priv->bbbus[unit], msgs, nmsgs);
249 reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0;
251 I915_WRITE(GMBUS0 + reg_offset, sc->reg0);
253 for (i = 0; i < nmsgs; i++) {
257 if ((msgs[i].flags & IIC_M_RD) != 0) {
258 I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_WAIT |
259 (i + 1 == nmsgs ? GMBUS_CYCLE_STOP : 0) |
260 (len << GMBUS_BYTE_COUNT_SHIFT) |
261 (msgs[i].slave << (GMBUS_SLAVE_ADDR_SHIFT - 1)) |
262 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
263 POSTING_READ(GMBUS2 + reg_offset);
267 if (_intel_wait_for(sc->drm_dev,
268 (I915_READ(GMBUS2 + reg_offset) &
269 (GMBUS_SATOER | GMBUS_HW_RDY)) != 0,
272 if ((I915_READ(GMBUS2 + reg_offset) &
276 val = I915_READ(GMBUS3 + reg_offset);
280 } while (--len != 0 && ++loop < 4);
285 val |= *buf++ << (8 * loop);
286 } while (--len != 0 && ++loop < 4);
288 I915_WRITE(GMBUS3 + reg_offset, val);
289 I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_WAIT |
290 (i + 1 == nmsgs ? GMBUS_CYCLE_STOP : 0) |
291 (msgs[i].len << GMBUS_BYTE_COUNT_SHIFT) |
292 (msgs[i].slave << (GMBUS_SLAVE_ADDR_SHIFT - 1)) |
293 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
294 POSTING_READ(GMBUS2+reg_offset);
297 if (_intel_wait_for(sc->drm_dev,
298 (I915_READ(GMBUS2 + reg_offset) &
299 (GMBUS_SATOER | GMBUS_HW_RDY)) != 0,
302 if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
307 val |= *buf++ << (8 * loop);
308 } while (--len != 0 && ++loop < 4);
310 I915_WRITE(GMBUS3 + reg_offset, val);
311 POSTING_READ(GMBUS2 + reg_offset);
315 if (i + 1 < nmsgs && _intel_wait_for(sc->drm_dev,
316 (I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER |
317 GMBUS_HW_WAIT_PHASE)) != 0,
320 if ((I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER) != 0)
326 /* Mark the GMBUS interface as disabled after waiting for idle.
327 * We will re-enable it at the start of the next xfer,
328 * till then let it sleep.
330 if (_intel_wait_for(dev,
331 (I915_READ(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0,
333 DRM_INFO("GMBUS timed out waiting for idle\n");
334 I915_WRITE(GMBUS0 + reg_offset, 0);
336 lockmgr(&dev_priv->gmbus_lock, LK_RELEASE);
340 /* Toggle the Software Clear Interrupt bit. This has the effect
341 * of resetting the GMBUS controller and so clearing the
342 * BUS_ERROR raised by the slave's NAK.
344 I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
345 I915_WRITE(GMBUS1 + reg_offset, 0);
350 DRM_INFO("GMBUS timed out, falling back to bit banging on pin %d [%s]\n",
351 sc->reg0 & 0xff, sc->name);
352 I915_WRITE(GMBUS0 + reg_offset, 0);
355 * Hardware may not support GMBUS over these pins?
356 * Try GPIO bitbanging instead.
358 sc->force_bit_dev = true;
360 error = intel_iic_quirk_xfer(dev_priv->bbbus[unit], msgs, nmsgs);
365 intel_gmbus_set_speed(device_t idev, int speed)
367 struct intel_iic_softc *sc;
369 sc = device_get_softc(device_get_parent(idev));
371 sc->reg0 = (sc->reg0 & ~(0x3 << 8)) | speed;
375 intel_gmbus_force_bit(device_t idev, bool force_bit)
377 struct intel_iic_softc *sc;
379 sc = device_get_softc(device_get_parent(idev));
380 sc->force_bit_dev = force_bit;
384 intel_iic_quirk_xfer(device_t idev, struct iic_msg *msgs, int nmsgs)
387 struct intel_iic_softc *sc;
388 struct drm_i915_private *dev_priv;
392 bridge_dev = device_get_parent(device_get_parent(idev));
393 sc = device_get_softc(bridge_dev);
394 dev_priv = sc->drm_dev->dev_private;
396 intel_iic_reset(sc->drm_dev);
397 intel_iic_quirk_set(dev_priv, true);
398 IICBB_SETSDA(bridge_dev, 1);
399 IICBB_SETSCL(bridge_dev, 1);
400 DELAY(I2C_RISEFALL_TIME);
402 for (i = 0; i < nmsgs - 1; i++) {
403 /* force use of repeated start instead of default stop+start */
404 msgs[i].flags |= IIC_M_NOSTOP;
406 ret = iicbus_transfer(idev, msgs, nmsgs);
407 IICBB_SETSDA(bridge_dev, 1);
408 IICBB_SETSCL(bridge_dev, 1);
409 intel_iic_quirk_set(dev_priv, false);
414 static const char *gpio_names[GMBUS_NUM_PORTS] = {
426 intel_gmbus_probe(device_t dev)
429 return (BUS_PROBE_SPECIFIC);
433 intel_gmbus_attach(device_t idev)
435 struct drm_i915_private *dev_priv;
436 struct intel_iic_softc *sc;
439 sc = device_get_softc(idev);
440 sc->drm_dev = device_get_softc(device_get_parent(idev));
441 dev_priv = sc->drm_dev->dev_private;
442 pin = device_get_unit(idev);
444 ksnprintf(sc->name, sizeof(sc->name), "gmbus bus %s", gpio_names[pin]);
445 device_set_desc(idev, sc->name);
447 /* By default use a conservative clock rate */
448 sc->reg0 = pin | GMBUS_RATE_100KHZ;
450 /* XXX force bit banging until GMBUS is fully debugged */
451 if (IS_GEN2(sc->drm_dev)) {
452 sc->force_bit_dev = true;
455 /* add bus interface device */
456 sc->iic_dev = device_add_child(idev, "iicbus", -1);
457 if (sc->iic_dev == NULL)
459 device_quiet(sc->iic_dev);
460 bus_generic_attach(idev);
466 intel_gmbus_detach(device_t idev)
468 struct intel_iic_softc *sc;
469 struct drm_i915_private *dev_priv;
473 sc = device_get_softc(idev);
474 u = device_get_unit(idev);
475 dev_priv = sc->drm_dev->dev_private;
478 bus_generic_detach(idev);
480 device_delete_child(idev, child);
486 intel_iicbb_probe(device_t dev)
489 return (BUS_PROBE_DEFAULT);
493 intel_iicbb_attach(device_t idev)
495 static const int map_pin_to_reg[] = {
506 struct intel_iic_softc *sc;
507 struct drm_i915_private *dev_priv;
510 sc = device_get_softc(idev);
511 sc->drm_dev = device_get_softc(device_get_parent(idev));
512 dev_priv = sc->drm_dev->dev_private;
513 pin = device_get_unit(idev);
515 ksnprintf(sc->name, sizeof(sc->name), "i915 iicbb %s", gpio_names[pin]);
516 device_set_desc(idev, sc->name);
518 sc->reg0 = pin | GMBUS_RATE_100KHZ;
519 sc->reg = map_pin_to_reg[pin];
520 if (HAS_PCH_SPLIT(dev_priv->dev))
521 sc->reg += PCH_GPIOA - GPIOA;
523 /* add generic bit-banging code */
524 sc->iic_dev = device_add_child(idev, "iicbb", -1);
525 if (sc->iic_dev == NULL)
527 device_quiet(sc->iic_dev);
528 bus_generic_attach(idev);
534 intel_iicbb_detach(device_t idev)
536 struct intel_iic_softc *sc;
539 sc = device_get_softc(idev);
541 bus_generic_detach(idev);
543 device_delete_child(idev, child);
547 static device_method_t intel_gmbus_methods[] = {
548 DEVMETHOD(device_probe, intel_gmbus_probe),
549 DEVMETHOD(device_attach, intel_gmbus_attach),
550 DEVMETHOD(device_detach, intel_gmbus_detach),
551 DEVMETHOD(iicbus_reset, intel_iicbus_reset),
552 DEVMETHOD(iicbus_transfer, intel_gmbus_transfer),
555 static driver_t intel_gmbus_driver = {
558 sizeof(struct intel_iic_softc)
560 static devclass_t intel_gmbus_devclass;
561 DRIVER_MODULE_ORDERED(intel_gmbus, drm, intel_gmbus_driver,
562 intel_gmbus_devclass, 0, 0, SI_ORDER_FIRST);
563 DRIVER_MODULE(iicbus, intel_gmbus, iicbus_driver, iicbus_devclass, 0, 0);
565 static device_method_t intel_iicbb_methods[] = {
566 DEVMETHOD(device_probe, intel_iicbb_probe),
567 DEVMETHOD(device_attach, intel_iicbb_attach),
568 DEVMETHOD(device_detach, intel_iicbb_detach),
570 DEVMETHOD(bus_add_child, bus_generic_add_child),
571 DEVMETHOD(bus_print_child, bus_generic_print_child),
573 DEVMETHOD(iicbb_callback, iicbus_null_callback),
574 DEVMETHOD(iicbb_reset, intel_iicbus_reset),
575 DEVMETHOD(iicbb_setsda, intel_iicbb_setsda),
576 DEVMETHOD(iicbb_setscl, intel_iicbb_setscl),
577 DEVMETHOD(iicbb_getsda, intel_iicbb_getsda),
578 DEVMETHOD(iicbb_getscl, intel_iicbb_getscl),
581 static driver_t intel_iicbb_driver = {
584 sizeof(struct intel_iic_softc)
586 static devclass_t intel_iicbb_devclass;
587 DRIVER_MODULE_ORDERED(intel_iicbb, drm, intel_iicbb_driver,
588 intel_iicbb_devclass, 0, 0, SI_ORDER_FIRST);
589 DRIVER_MODULE(iicbb, intel_iicbb, iicbb_driver, iicbb_devclass, 0, 0);
592 intel_setup_gmbus(struct drm_device *dev)
594 struct drm_i915_private *dev_priv;
598 dev_priv = dev->dev_private;
599 lockinit(&dev_priv->gmbus_lock, "gmbus", 0, LK_CANRECURSE);
600 dev_priv->gmbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
601 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
602 dev_priv->bbbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
603 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
604 dev_priv->gmbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
605 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
606 dev_priv->bbbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
607 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
610 * The Giant there is recursed, most likely. Normally, the
611 * intel_setup_gmbus() is called from the attach method of the
615 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
617 * Initialized bbbus_bridge before gmbus_bridge, since
618 * gmbus may decide to force quirk transfer in the
621 dev_priv->bbbus_bridge[i] = device_add_child(dev->device,
623 if (dev_priv->bbbus_bridge[i] == NULL) {
624 DRM_ERROR("bbbus bridge %d creation failed\n", i);
628 device_quiet(dev_priv->bbbus_bridge[i]);
629 ret = device_probe_and_attach(dev_priv->bbbus_bridge[i]);
631 DRM_ERROR("bbbus bridge %d attach failed, %d\n", i,
636 iic_dev = device_find_child(dev_priv->bbbus_bridge[i], "iicbb",
638 if (iic_dev == NULL) {
639 DRM_ERROR("bbbus bridge doesn't have iicbb child\n");
642 iic_dev = device_find_child(iic_dev, "iicbus", -1);
643 if (iic_dev == NULL) {
645 "bbbus bridge doesn't have iicbus grandchild\n");
649 dev_priv->bbbus[i] = iic_dev;
651 dev_priv->gmbus_bridge[i] = device_add_child(dev->device,
653 if (dev_priv->gmbus_bridge[i] == NULL) {
654 DRM_ERROR("gmbus bridge %d creation failed\n", i);
658 device_quiet(dev_priv->gmbus_bridge[i]);
659 ret = device_probe_and_attach(dev_priv->gmbus_bridge[i]);
661 DRM_ERROR("gmbus bridge %d attach failed, %d\n", i,
667 iic_dev = device_find_child(dev_priv->gmbus_bridge[i],
669 if (iic_dev == NULL) {
670 DRM_ERROR("gmbus bridge doesn't have iicbus child\n");
673 dev_priv->gmbus[i] = iic_dev;
675 intel_iic_reset(dev);
682 intel_teardown_gmbus_m(dev, i);
688 intel_teardown_gmbus_m(struct drm_device *dev, int m)
690 struct drm_i915_private *dev_priv;
692 dev_priv = dev->dev_private;
694 drm_free(dev_priv->gmbus, DRM_MEM_DRIVER);
695 dev_priv->gmbus = NULL;
696 drm_free(dev_priv->bbbus, DRM_MEM_DRIVER);
697 dev_priv->bbbus = NULL;
698 drm_free(dev_priv->gmbus_bridge, DRM_MEM_DRIVER);
699 dev_priv->gmbus_bridge = NULL;
700 drm_free(dev_priv->bbbus_bridge, DRM_MEM_DRIVER);
701 dev_priv->bbbus_bridge = NULL;
702 lockuninit(&dev_priv->gmbus_lock);
706 intel_teardown_gmbus(struct drm_device *dev)
710 intel_teardown_gmbus_m(dev, GMBUS_NUM_PORTS);