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 #include <sys/cdefs.h>
57 __FBSDID("$FreeBSD$");
59 #include <dev/drm2/drmP.h>
60 #include <dev/drm2/drm.h>
61 #include <dev/drm2/i915/i915_drm.h>
62 #include <dev/drm2/i915/i915_drv.h>
63 #include <dev/drm2/i915/intel_drv.h>
64 #include <dev/iicbus/iic.h>
65 #include <dev/iicbus/iiconf.h>
66 #include <dev/iicbus/iicbus.h>
67 #include "iicbus_if.h"
70 static int intel_iic_quirk_xfer(device_t idev, struct iic_msg *msgs, int nmsgs);
71 static void intel_teardown_gmbus_m(struct drm_device *dev, int m);
73 /* Intel GPIO access functions */
75 #define I2C_RISEFALL_TIME 10
77 struct intel_iic_softc {
78 struct drm_device *drm_dev;
87 intel_iic_quirk_set(struct drm_i915_private *dev_priv, bool enable)
91 /* When using bit bashing for I2C, this bit needs to be set to 1 */
92 if (!IS_PINEVIEW(dev_priv->dev))
95 val = I915_READ(DSPCLK_GATE_D);
97 val |= DPCUNIT_CLOCK_GATE_DISABLE;
99 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
100 I915_WRITE(DSPCLK_GATE_D, val);
104 intel_iic_get_reserved(device_t idev)
106 struct intel_iic_softc *sc;
107 struct drm_device *dev;
108 struct drm_i915_private *dev_priv;
111 sc = device_get_softc(idev);
113 dev_priv = dev->dev_private;
115 if (!IS_I830(dev) && !IS_845G(dev)) {
116 reserved = I915_READ_NOTRACE(sc->reg) &
117 (GPIO_DATA_PULLUP_DISABLE | GPIO_CLOCK_PULLUP_DISABLE);
126 intel_iic_reset(struct drm_device *dev)
128 struct drm_i915_private *dev_priv;
130 dev_priv = dev->dev_private;
131 if (HAS_PCH_SPLIT(dev))
132 I915_WRITE(PCH_GMBUS0, 0);
134 I915_WRITE(GMBUS0, 0);
138 intel_iicbus_reset(device_t idev, u_char speed, u_char addr, u_char *oldaddr)
140 struct intel_iic_softc *sc;
141 struct drm_device *dev;
143 sc = device_get_softc(idev);
146 intel_iic_reset(dev);
151 intel_iicbb_setsda(device_t idev, int val)
153 struct intel_iic_softc *sc;
154 struct drm_i915_private *dev_priv;
158 sc = device_get_softc(idev);
159 dev_priv = sc->drm_dev->dev_private;
161 reserved = intel_iic_get_reserved(idev);
163 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
165 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
168 I915_WRITE_NOTRACE(sc->reg, reserved | data_bits);
169 POSTING_READ(sc->reg);
173 intel_iicbb_setscl(device_t idev, int val)
175 struct intel_iic_softc *sc;
176 struct drm_i915_private *dev_priv;
177 u32 clock_bits, reserved;
179 sc = device_get_softc(idev);
180 dev_priv = sc->drm_dev->dev_private;
182 reserved = intel_iic_get_reserved(idev);
184 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
186 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
189 I915_WRITE_NOTRACE(sc->reg, reserved | clock_bits);
190 POSTING_READ(sc->reg);
194 intel_iicbb_getsda(device_t idev)
196 struct intel_iic_softc *sc;
197 struct drm_i915_private *dev_priv;
200 sc = device_get_softc(idev);
201 dev_priv = sc->drm_dev->dev_private;
203 reserved = intel_iic_get_reserved(idev);
205 I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_DATA_DIR_MASK);
206 I915_WRITE_NOTRACE(sc->reg, reserved);
207 return ((I915_READ_NOTRACE(sc->reg) & GPIO_DATA_VAL_IN) != 0);
211 intel_iicbb_getscl(device_t idev)
213 struct intel_iic_softc *sc;
214 struct drm_i915_private *dev_priv;
217 sc = device_get_softc(idev);
218 dev_priv = sc->drm_dev->dev_private;
220 reserved = intel_iic_get_reserved(idev);
222 I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_CLOCK_DIR_MASK);
223 I915_WRITE_NOTRACE(sc->reg, reserved);
224 return ((I915_READ_NOTRACE(sc->reg) & GPIO_CLOCK_VAL_IN) != 0);
228 intel_gmbus_transfer(device_t idev, struct iic_msg *msgs, uint32_t nmsgs)
230 struct intel_iic_softc *sc;
231 struct drm_i915_private *dev_priv;
233 int error, i, reg_offset, unit;
237 sc = device_get_softc(idev);
238 dev_priv = sc->drm_dev->dev_private;
239 unit = device_get_unit(idev);
241 sx_xlock(&dev_priv->gmbus_sx);
242 if (sc->force_bit_dev) {
243 error = intel_iic_quirk_xfer(dev_priv->bbbus[unit], msgs, nmsgs);
247 reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0;
249 I915_WRITE(GMBUS0 + reg_offset, sc->reg0);
251 for (i = 0; i < nmsgs; i++) {
255 if ((msgs[i].flags & IIC_M_RD) != 0) {
256 I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_WAIT |
257 (i + 1 == nmsgs ? GMBUS_CYCLE_STOP : 0) |
258 (len << GMBUS_BYTE_COUNT_SHIFT) |
259 (msgs[i].slave << (GMBUS_SLAVE_ADDR_SHIFT - 1)) |
260 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
261 POSTING_READ(GMBUS2 + reg_offset);
265 if (_intel_wait_for(sc->drm_dev,
266 (I915_READ(GMBUS2 + reg_offset) &
267 (GMBUS_SATOER | GMBUS_HW_RDY)) != 0,
270 if ((I915_READ(GMBUS2 + reg_offset) &
274 val = I915_READ(GMBUS3 + reg_offset);
278 } while (--len != 0 && ++loop < 4);
283 val |= *buf++ << (8 * loop);
284 } while (--len != 0 && ++loop < 4);
286 I915_WRITE(GMBUS3 + reg_offset, val);
287 I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_WAIT |
288 (i + 1 == nmsgs ? GMBUS_CYCLE_STOP : 0) |
289 (msgs[i].len << GMBUS_BYTE_COUNT_SHIFT) |
290 (msgs[i].slave << (GMBUS_SLAVE_ADDR_SHIFT - 1)) |
291 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
292 POSTING_READ(GMBUS2+reg_offset);
295 if (_intel_wait_for(sc->drm_dev,
296 (I915_READ(GMBUS2 + reg_offset) &
297 (GMBUS_SATOER | GMBUS_HW_RDY)) != 0,
300 if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
305 val |= *buf++ << (8 * loop);
306 } while (--len != 0 && ++loop < 4);
308 I915_WRITE(GMBUS3 + reg_offset, val);
309 POSTING_READ(GMBUS2 + reg_offset);
313 if (i + 1 < nmsgs && _intel_wait_for(sc->drm_dev,
314 (I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER |
315 GMBUS_HW_WAIT_PHASE)) != 0,
318 if ((I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER) != 0)
324 /* Mark the GMBUS interface as disabled after waiting for idle.
325 * We will re-enable it at the start of the next xfer,
326 * till then let it sleep.
328 if (_intel_wait_for(dev,
329 (I915_READ(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0,
331 DRM_INFO("GMBUS timed out waiting for idle\n");
332 I915_WRITE(GMBUS0 + reg_offset, 0);
334 sx_xunlock(&dev_priv->gmbus_sx);
338 /* Toggle the Software Clear Interrupt bit. This has the effect
339 * of resetting the GMBUS controller and so clearing the
340 * BUS_ERROR raised by the slave's NAK.
342 I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
343 I915_WRITE(GMBUS1 + reg_offset, 0);
348 DRM_INFO("GMBUS timed out, falling back to bit banging on pin %d [%s]\n",
349 sc->reg0 & 0xff, sc->name);
350 I915_WRITE(GMBUS0 + reg_offset, 0);
353 * Hardware may not support GMBUS over these pins?
354 * Try GPIO bitbanging instead.
356 sc->force_bit_dev = true;
358 error = intel_iic_quirk_xfer(dev_priv->bbbus[unit], msgs, nmsgs);
363 intel_gmbus_set_speed(device_t idev, int speed)
365 struct intel_iic_softc *sc;
367 sc = device_get_softc(device_get_parent(idev));
369 sc->reg0 = (sc->reg0 & ~(0x3 << 8)) | speed;
373 intel_gmbus_force_bit(device_t idev, bool force_bit)
375 struct intel_iic_softc *sc;
377 sc = device_get_softc(device_get_parent(idev));
378 sc->force_bit_dev = force_bit;
382 intel_iic_quirk_xfer(device_t idev, struct iic_msg *msgs, int nmsgs)
385 struct intel_iic_softc *sc;
386 struct drm_i915_private *dev_priv;
390 bridge_dev = device_get_parent(device_get_parent(idev));
391 sc = device_get_softc(bridge_dev);
392 dev_priv = sc->drm_dev->dev_private;
394 intel_iic_reset(sc->drm_dev);
395 intel_iic_quirk_set(dev_priv, true);
396 IICBB_SETSDA(bridge_dev, 1);
397 IICBB_SETSCL(bridge_dev, 1);
398 DELAY(I2C_RISEFALL_TIME);
400 for (i = 0; i < nmsgs - 1; i++) {
401 /* force use of repeated start instead of default stop+start */
402 msgs[i].flags |= IIC_M_NOSTOP;
404 ret = iicbus_transfer(idev, msgs, nmsgs);
405 IICBB_SETSDA(bridge_dev, 1);
406 IICBB_SETSCL(bridge_dev, 1);
407 intel_iic_quirk_set(dev_priv, false);
412 static const char *gpio_names[GMBUS_NUM_PORTS] = {
424 intel_gmbus_probe(device_t dev)
427 return (BUS_PROBE_SPECIFIC);
431 intel_gmbus_attach(device_t idev)
433 struct drm_i915_private *dev_priv;
434 struct intel_iic_softc *sc;
437 sc = device_get_softc(idev);
438 sc->drm_dev = device_get_softc(device_get_parent(idev));
439 dev_priv = sc->drm_dev->dev_private;
440 pin = device_get_unit(idev);
442 snprintf(sc->name, sizeof(sc->name), "gmbus bus %s", gpio_names[pin]);
443 device_set_desc(idev, sc->name);
445 /* By default use a conservative clock rate */
446 sc->reg0 = pin | GMBUS_RATE_100KHZ;
448 /* XXX force bit banging until GMBUS is fully debugged */
449 if (IS_GEN2(sc->drm_dev)) {
450 sc->force_bit_dev = true;
453 /* add bus interface device */
454 sc->iic_dev = device_add_child(idev, "iicbus", -1);
455 if (sc->iic_dev == NULL)
457 device_quiet(sc->iic_dev);
458 bus_generic_attach(idev);
464 intel_gmbus_detach(device_t idev)
466 struct intel_iic_softc *sc;
467 struct drm_i915_private *dev_priv;
471 sc = device_get_softc(idev);
472 u = device_get_unit(idev);
473 dev_priv = sc->drm_dev->dev_private;
476 bus_generic_detach(idev);
478 device_delete_child(idev, child);
484 intel_iicbb_probe(device_t dev)
487 return (BUS_PROBE_DEFAULT);
491 intel_iicbb_attach(device_t idev)
493 static const int map_pin_to_reg[] = {
504 struct intel_iic_softc *sc;
505 struct drm_i915_private *dev_priv;
508 sc = device_get_softc(idev);
509 sc->drm_dev = device_get_softc(device_get_parent(idev));
510 dev_priv = sc->drm_dev->dev_private;
511 pin = device_get_unit(idev);
513 snprintf(sc->name, sizeof(sc->name), "i915 iicbb %s", gpio_names[pin]);
514 device_set_desc(idev, sc->name);
516 sc->reg0 = pin | GMBUS_RATE_100KHZ;
517 sc->reg = map_pin_to_reg[pin];
518 if (HAS_PCH_SPLIT(dev_priv->dev))
519 sc->reg += PCH_GPIOA - GPIOA;
521 /* add generic bit-banging code */
522 sc->iic_dev = device_add_child(idev, "iicbb", -1);
523 if (sc->iic_dev == NULL)
525 device_quiet(sc->iic_dev);
526 bus_generic_attach(idev);
532 intel_iicbb_detach(device_t idev)
534 struct intel_iic_softc *sc;
537 sc = device_get_softc(idev);
539 bus_generic_detach(idev);
541 device_delete_child(idev, child);
545 static device_method_t intel_gmbus_methods[] = {
546 DEVMETHOD(device_probe, intel_gmbus_probe),
547 DEVMETHOD(device_attach, intel_gmbus_attach),
548 DEVMETHOD(device_detach, intel_gmbus_detach),
549 DEVMETHOD(iicbus_reset, intel_iicbus_reset),
550 DEVMETHOD(iicbus_transfer, intel_gmbus_transfer),
553 static driver_t intel_gmbus_driver = {
556 sizeof(struct intel_iic_softc)
558 static devclass_t intel_gmbus_devclass;
559 DRIVER_MODULE_ORDERED(intel_gmbus, drmn, intel_gmbus_driver,
560 intel_gmbus_devclass, 0, 0, SI_ORDER_FIRST);
561 DRIVER_MODULE(iicbus, intel_gmbus, iicbus_driver, iicbus_devclass, 0, 0);
563 static device_method_t intel_iicbb_methods[] = {
564 DEVMETHOD(device_probe, intel_iicbb_probe),
565 DEVMETHOD(device_attach, intel_iicbb_attach),
566 DEVMETHOD(device_detach, intel_iicbb_detach),
568 DEVMETHOD(bus_add_child, bus_generic_add_child),
569 DEVMETHOD(bus_print_child, bus_generic_print_child),
571 DEVMETHOD(iicbb_callback, iicbus_null_callback),
572 DEVMETHOD(iicbb_reset, intel_iicbus_reset),
573 DEVMETHOD(iicbb_setsda, intel_iicbb_setsda),
574 DEVMETHOD(iicbb_setscl, intel_iicbb_setscl),
575 DEVMETHOD(iicbb_getsda, intel_iicbb_getsda),
576 DEVMETHOD(iicbb_getscl, intel_iicbb_getscl),
579 static driver_t intel_iicbb_driver = {
582 sizeof(struct intel_iic_softc)
584 static devclass_t intel_iicbb_devclass;
585 DRIVER_MODULE_ORDERED(intel_iicbb, drmn, intel_iicbb_driver,
586 intel_iicbb_devclass, 0, 0, SI_ORDER_FIRST);
587 DRIVER_MODULE(iicbb, intel_iicbb, iicbb_driver, iicbb_devclass, 0, 0);
590 intel_setup_gmbus(struct drm_device *dev)
592 struct drm_i915_private *dev_priv;
596 dev_priv = dev->dev_private;
597 sx_init(&dev_priv->gmbus_sx, "gmbus");
598 dev_priv->gmbus_bridge = malloc(sizeof(device_t) * GMBUS_NUM_PORTS,
599 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
600 dev_priv->bbbus_bridge = malloc(sizeof(device_t) * GMBUS_NUM_PORTS,
601 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
602 dev_priv->gmbus = malloc(sizeof(device_t) * GMBUS_NUM_PORTS,
603 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
604 dev_priv->bbbus = malloc(sizeof(device_t) * GMBUS_NUM_PORTS,
605 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
608 * The Giant there is recursed, most likely. Normally, the
609 * intel_setup_gmbus() is called from the attach method of the
613 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
615 * Initialized bbbus_bridge before gmbus_bridge, since
616 * gmbus may decide to force quirk transfer in the
619 dev_priv->bbbus_bridge[i] = device_add_child(dev->device,
621 if (dev_priv->bbbus_bridge[i] == NULL) {
622 DRM_ERROR("bbbus bridge %d creation failed\n", i);
626 device_quiet(dev_priv->bbbus_bridge[i]);
627 ret = device_probe_and_attach(dev_priv->bbbus_bridge[i]);
629 DRM_ERROR("bbbus bridge %d attach failed, %d\n", i,
634 iic_dev = device_find_child(dev_priv->bbbus_bridge[i], "iicbb",
636 if (iic_dev == NULL) {
637 DRM_ERROR("bbbus bridge doesn't have iicbb child\n");
640 iic_dev = device_find_child(iic_dev, "iicbus", -1);
641 if (iic_dev == NULL) {
643 "bbbus bridge doesn't have iicbus grandchild\n");
647 dev_priv->bbbus[i] = iic_dev;
649 dev_priv->gmbus_bridge[i] = device_add_child(dev->device,
651 if (dev_priv->gmbus_bridge[i] == NULL) {
652 DRM_ERROR("gmbus bridge %d creation failed\n", i);
656 device_quiet(dev_priv->gmbus_bridge[i]);
657 ret = device_probe_and_attach(dev_priv->gmbus_bridge[i]);
659 DRM_ERROR("gmbus bridge %d attach failed, %d\n", i,
665 iic_dev = device_find_child(dev_priv->gmbus_bridge[i],
667 if (iic_dev == NULL) {
668 DRM_ERROR("gmbus bridge doesn't have iicbus child\n");
671 dev_priv->gmbus[i] = iic_dev;
673 intel_iic_reset(dev);
680 intel_teardown_gmbus_m(dev, i);
686 intel_teardown_gmbus_m(struct drm_device *dev, int m)
688 struct drm_i915_private *dev_priv;
690 dev_priv = dev->dev_private;
692 free(dev_priv->gmbus, DRM_MEM_DRIVER);
693 dev_priv->gmbus = NULL;
694 free(dev_priv->bbbus, DRM_MEM_DRIVER);
695 dev_priv->bbbus = NULL;
696 free(dev_priv->gmbus_bridge, DRM_MEM_DRIVER);
697 dev_priv->gmbus_bridge = NULL;
698 free(dev_priv->bbbus_bridge, DRM_MEM_DRIVER);
699 dev_priv->bbbus_bridge = NULL;
700 sx_destroy(&dev_priv->gmbus_sx);
704 intel_teardown_gmbus(struct drm_device *dev)
708 intel_teardown_gmbus_m(dev, GMBUS_NUM_PORTS);