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) |
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) |
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 /* convert slave addresses to format expected by iicbb */
401 for (i = 0; i < nmsgs; i++) {
403 /* force use of repeated start instead of default stop+start */
404 if (i != (nmsgs - 1))
405 msgs[i].flags |= IIC_M_NOSTOP;
407 ret = iicbus_transfer(idev, msgs, nmsgs);
408 /* restore the addresses */
409 for (i = 0; i < nmsgs; i++)
411 IICBB_SETSDA(bridge_dev, 1);
412 IICBB_SETSCL(bridge_dev, 1);
413 intel_iic_quirk_set(dev_priv, false);
418 static const char *gpio_names[GMBUS_NUM_PORTS] = {
430 intel_gmbus_probe(device_t dev)
433 return (BUS_PROBE_SPECIFIC);
437 intel_gmbus_attach(device_t idev)
439 struct drm_i915_private *dev_priv;
440 struct intel_iic_softc *sc;
443 sc = device_get_softc(idev);
444 sc->drm_dev = device_get_softc(device_get_parent(idev));
445 dev_priv = sc->drm_dev->dev_private;
446 pin = device_get_unit(idev);
448 snprintf(sc->name, sizeof(sc->name), "gmbus bus %s", gpio_names[pin]);
449 device_set_desc(idev, sc->name);
451 /* By default use a conservative clock rate */
452 sc->reg0 = pin | GMBUS_RATE_100KHZ;
454 /* XXX force bit banging until GMBUS is fully debugged */
455 if (IS_GEN2(sc->drm_dev)) {
456 sc->force_bit_dev = true;
459 /* add bus interface device */
460 sc->iic_dev = device_add_child(idev, "iicbus", -1);
461 if (sc->iic_dev == NULL)
463 device_quiet(sc->iic_dev);
464 bus_generic_attach(idev);
470 intel_gmbus_detach(device_t idev)
472 struct intel_iic_softc *sc;
473 struct drm_i915_private *dev_priv;
477 sc = device_get_softc(idev);
478 u = device_get_unit(idev);
479 dev_priv = sc->drm_dev->dev_private;
482 bus_generic_detach(idev);
484 device_delete_child(idev, child);
490 intel_iicbb_probe(device_t dev)
493 return (BUS_PROBE_DEFAULT);
497 intel_iicbb_attach(device_t idev)
499 static const int map_pin_to_reg[] = {
510 struct intel_iic_softc *sc;
511 struct drm_i915_private *dev_priv;
514 sc = device_get_softc(idev);
515 sc->drm_dev = device_get_softc(device_get_parent(idev));
516 dev_priv = sc->drm_dev->dev_private;
517 pin = device_get_unit(idev);
519 snprintf(sc->name, sizeof(sc->name), "i915 iicbb %s", gpio_names[pin]);
520 device_set_desc(idev, sc->name);
522 sc->reg0 = pin | GMBUS_RATE_100KHZ;
523 sc->reg = map_pin_to_reg[pin];
524 if (HAS_PCH_SPLIT(dev_priv->dev))
525 sc->reg += PCH_GPIOA - GPIOA;
527 /* add generic bit-banging code */
528 sc->iic_dev = device_add_child(idev, "iicbb", -1);
529 if (sc->iic_dev == NULL)
531 device_quiet(sc->iic_dev);
532 bus_generic_attach(idev);
538 intel_iicbb_detach(device_t idev)
540 struct intel_iic_softc *sc;
543 sc = device_get_softc(idev);
545 bus_generic_detach(idev);
547 device_delete_child(idev, child);
551 static device_method_t intel_gmbus_methods[] = {
552 DEVMETHOD(device_probe, intel_gmbus_probe),
553 DEVMETHOD(device_attach, intel_gmbus_attach),
554 DEVMETHOD(device_detach, intel_gmbus_detach),
555 DEVMETHOD(iicbus_reset, intel_iicbus_reset),
556 DEVMETHOD(iicbus_transfer, intel_gmbus_transfer),
559 static driver_t intel_gmbus_driver = {
562 sizeof(struct intel_iic_softc)
564 static devclass_t intel_gmbus_devclass;
565 DRIVER_MODULE_ORDERED(intel_gmbus, drmn, intel_gmbus_driver,
566 intel_gmbus_devclass, 0, 0, SI_ORDER_FIRST);
567 DRIVER_MODULE(iicbus, intel_gmbus, iicbus_driver, iicbus_devclass, 0, 0);
569 static device_method_t intel_iicbb_methods[] = {
570 DEVMETHOD(device_probe, intel_iicbb_probe),
571 DEVMETHOD(device_attach, intel_iicbb_attach),
572 DEVMETHOD(device_detach, intel_iicbb_detach),
574 DEVMETHOD(bus_add_child, bus_generic_add_child),
575 DEVMETHOD(bus_print_child, bus_generic_print_child),
577 DEVMETHOD(iicbb_callback, iicbus_null_callback),
578 DEVMETHOD(iicbb_reset, intel_iicbus_reset),
579 DEVMETHOD(iicbb_setsda, intel_iicbb_setsda),
580 DEVMETHOD(iicbb_setscl, intel_iicbb_setscl),
581 DEVMETHOD(iicbb_getsda, intel_iicbb_getsda),
582 DEVMETHOD(iicbb_getscl, intel_iicbb_getscl),
585 static driver_t intel_iicbb_driver = {
588 sizeof(struct intel_iic_softc)
590 static devclass_t intel_iicbb_devclass;
591 DRIVER_MODULE_ORDERED(intel_iicbb, drmn, intel_iicbb_driver,
592 intel_iicbb_devclass, 0, 0, SI_ORDER_FIRST);
593 DRIVER_MODULE(iicbb, intel_iicbb, iicbb_driver, iicbb_devclass, 0, 0);
596 intel_setup_gmbus(struct drm_device *dev)
598 struct drm_i915_private *dev_priv;
602 dev_priv = dev->dev_private;
603 sx_init(&dev_priv->gmbus_sx, "gmbus");
604 dev_priv->gmbus_bridge = malloc(sizeof(device_t) * GMBUS_NUM_PORTS,
605 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
606 dev_priv->bbbus_bridge = malloc(sizeof(device_t) * GMBUS_NUM_PORTS,
607 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
608 dev_priv->gmbus = malloc(sizeof(device_t) * GMBUS_NUM_PORTS,
609 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
610 dev_priv->bbbus = malloc(sizeof(device_t) * GMBUS_NUM_PORTS,
611 DRM_MEM_DRIVER, M_WAITOK | M_ZERO);
614 * The Giant there is recursed, most likely. Normally, the
615 * intel_setup_gmbus() is called from the attach method of the
619 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
621 * Initialized bbbus_bridge before gmbus_bridge, since
622 * gmbus may decide to force quirk transfer in the
625 dev_priv->bbbus_bridge[i] = device_add_child(dev->device,
627 if (dev_priv->bbbus_bridge[i] == NULL) {
628 DRM_ERROR("bbbus bridge %d creation failed\n", i);
632 device_quiet(dev_priv->bbbus_bridge[i]);
633 ret = device_probe_and_attach(dev_priv->bbbus_bridge[i]);
635 DRM_ERROR("bbbus bridge %d attach failed, %d\n", i,
640 iic_dev = device_find_child(dev_priv->bbbus_bridge[i], "iicbb",
642 if (iic_dev == NULL) {
643 DRM_ERROR("bbbus bridge doesn't have iicbb child\n");
646 iic_dev = device_find_child(iic_dev, "iicbus", -1);
647 if (iic_dev == NULL) {
649 "bbbus bridge doesn't have iicbus grandchild\n");
653 dev_priv->bbbus[i] = iic_dev;
655 dev_priv->gmbus_bridge[i] = device_add_child(dev->device,
657 if (dev_priv->gmbus_bridge[i] == NULL) {
658 DRM_ERROR("gmbus bridge %d creation failed\n", i);
662 device_quiet(dev_priv->gmbus_bridge[i]);
663 ret = device_probe_and_attach(dev_priv->gmbus_bridge[i]);
665 DRM_ERROR("gmbus bridge %d attach failed, %d\n", i,
671 iic_dev = device_find_child(dev_priv->gmbus_bridge[i],
673 if (iic_dev == NULL) {
674 DRM_ERROR("gmbus bridge doesn't have iicbus child\n");
677 dev_priv->gmbus[i] = iic_dev;
679 intel_iic_reset(dev);
686 intel_teardown_gmbus_m(dev, i);
692 intel_teardown_gmbus_m(struct drm_device *dev, int m)
694 struct drm_i915_private *dev_priv;
696 dev_priv = dev->dev_private;
698 free(dev_priv->gmbus, DRM_MEM_DRIVER);
699 dev_priv->gmbus = NULL;
700 free(dev_priv->bbbus, DRM_MEM_DRIVER);
701 dev_priv->bbbus = NULL;
702 free(dev_priv->gmbus_bridge, DRM_MEM_DRIVER);
703 dev_priv->gmbus_bridge = NULL;
704 free(dev_priv->bbbus_bridge, DRM_MEM_DRIVER);
705 dev_priv->bbbus_bridge = NULL;
706 sx_destroy(&dev_priv->gmbus_sx);
710 intel_teardown_gmbus(struct drm_device *dev)
714 intel_teardown_gmbus_m(dev, GMBUS_NUM_PORTS);