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
57 #include <sys/mplock2.h>
59 #include <linux/i2c.h>
60 #include <linux/export.h>
62 #include "intel_drv.h"
63 #include <drm/i915_drm.h>
66 #include <bus/iicbus/iic.h>
67 #include <bus/iicbus/iiconf.h>
68 #include <bus/iicbus/iicbus.h>
69 #include "iicbus_if.h"
77 /* Map gmbus pin pairs to names and registers. */
78 static const struct gmbus_pin gmbus_pins[] = {
79 [GMBUS_PIN_SSC] = { "ssc", GPIOB },
80 [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
81 [GMBUS_PIN_PANEL] = { "panel", GPIOC },
82 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
83 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
84 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
87 static const struct gmbus_pin gmbus_pins_bdw[] = {
88 [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
89 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
90 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
91 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
94 static const struct gmbus_pin gmbus_pins_skl[] = {
95 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
96 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
97 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
100 static const struct gmbus_pin gmbus_pins_bxt[] = {
101 [GMBUS_PIN_1_BXT] = { "dpb", PCH_GPIOB },
102 [GMBUS_PIN_2_BXT] = { "dpc", PCH_GPIOC },
103 [GMBUS_PIN_3_BXT] = { "misc", PCH_GPIOD },
106 /* pin is expected to be valid */
107 static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
110 if (IS_BROXTON(dev_priv))
111 return &gmbus_pins_bxt[pin];
112 else if (IS_SKYLAKE(dev_priv))
113 return &gmbus_pins_skl[pin];
114 else if (IS_BROADWELL(dev_priv))
115 return &gmbus_pins_bdw[pin];
117 return &gmbus_pins[pin];
120 bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
125 if (IS_BROXTON(dev_priv))
126 size = ARRAY_SIZE(gmbus_pins_bxt);
127 else if (IS_SKYLAKE(dev_priv))
128 size = ARRAY_SIZE(gmbus_pins_skl);
129 else if (IS_BROADWELL(dev_priv))
130 size = ARRAY_SIZE(gmbus_pins_bdw);
132 size = ARRAY_SIZE(gmbus_pins);
134 return pin < size && get_gmbus_pin(dev_priv, pin)->reg;
137 /* Intel GPIO access functions */
139 #define I2C_RISEFALL_TIME 10
142 intel_i2c_reset(struct drm_device *dev)
144 struct drm_i915_private *dev_priv = dev->dev_private;
146 I915_WRITE(GMBUS0, 0);
147 I915_WRITE(GMBUS4, 0);
150 static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
154 /* When using bit bashing for I2C, this bit needs to be set to 1 */
155 if (!IS_PINEVIEW(dev_priv->dev))
158 val = I915_READ(DSPCLK_GATE_D);
160 val |= DPCUNIT_CLOCK_GATE_DISABLE;
162 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
163 I915_WRITE(DSPCLK_GATE_D, val);
166 static u32 get_reserved(device_t idev)
168 struct intel_iic_softc *sc = device_get_softc(idev);
169 struct drm_device *dev = sc->drm_dev;
170 struct drm_i915_private *dev_priv;
173 dev_priv = dev->dev_private;
175 /* On most chips, these bits must be preserved in software. */
176 if (!IS_I830(dev) && !IS_845G(dev))
177 reserved = I915_READ_NOTRACE(sc->reg) &
178 (GPIO_DATA_PULLUP_DISABLE |
179 GPIO_CLOCK_PULLUP_DISABLE);
184 static int get_clock(device_t idev)
186 struct intel_iic_softc *sc;
187 struct drm_i915_private *dev_priv;
190 sc = device_get_softc(idev);
191 dev_priv = sc->drm_dev->dev_private;
193 reserved = get_reserved(idev);
195 I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_CLOCK_DIR_MASK);
196 I915_WRITE_NOTRACE(sc->reg, reserved);
197 return ((I915_READ_NOTRACE(sc->reg) & GPIO_CLOCK_VAL_IN) != 0);
200 static int get_data(device_t idev)
202 struct intel_iic_softc *sc;
203 struct drm_i915_private *dev_priv;
206 sc = device_get_softc(idev);
207 dev_priv = sc->drm_dev->dev_private;
209 reserved = get_reserved(idev);
211 I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_DATA_DIR_MASK);
212 I915_WRITE_NOTRACE(sc->reg, reserved);
213 return ((I915_READ_NOTRACE(sc->reg) & GPIO_DATA_VAL_IN) != 0);
217 intel_iicbus_reset(device_t idev, u_char speed, u_char addr, u_char *oldaddr)
219 struct intel_iic_softc *sc;
220 struct drm_device *dev;
222 sc = device_get_softc(idev);
225 intel_i2c_reset(dev);
229 static void set_clock(device_t idev, int val)
231 struct intel_iic_softc *sc;
232 struct drm_i915_private *dev_priv;
233 u32 clock_bits, reserved;
235 sc = device_get_softc(idev);
236 dev_priv = sc->drm_dev->dev_private;
238 reserved = get_reserved(idev);
240 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
242 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
245 I915_WRITE_NOTRACE(sc->reg, reserved | clock_bits);
246 POSTING_READ(sc->reg);
249 static void set_data(device_t idev, int val)
251 struct intel_iic_softc *sc;
252 struct drm_i915_private *dev_priv;
256 sc = device_get_softc(idev);
257 dev_priv = sc->drm_dev->dev_private;
259 reserved = get_reserved(idev);
262 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
264 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
267 I915_WRITE_NOTRACE(sc->reg, reserved | data_bits);
268 POSTING_READ(sc->reg);
271 static const char *gpio_names[GMBUS_NUM_PINS] = {
281 intel_gpio_setup(device_t idev)
283 static const int map_pin_to_reg[] = {
294 struct intel_iic_softc *sc;
295 struct drm_i915_private *dev_priv;
298 sc = device_get_softc(idev);
299 sc->drm_dev = device_get_softc(device_get_parent(idev));
300 dev_priv = sc->drm_dev->dev_private;
301 pin = device_get_unit(idev);
303 ksnprintf(sc->name, sizeof(sc->name), "i915 iicbb %s", gpio_names[pin]);
304 device_set_desc(idev, sc->name);
306 sc->reg0 = pin | GMBUS_RATE_100KHZ;
307 sc->reg = dev_priv->gpio_mmio_base + map_pin_to_reg[pin];
309 /* add generic bit-banging code */
310 sc->iic_dev = device_add_child(idev, "iicbb", -1);
311 if (sc->iic_dev == NULL)
313 device_quiet(sc->iic_dev);
314 bus_generic_attach(idev);
320 intel_i2c_quirk_xfer(device_t idev, struct iic_msg *msgs, int nmsgs)
323 struct intel_iic_softc *sc;
324 struct drm_i915_private *dev_priv;
328 bridge_dev = device_get_parent(device_get_parent(idev));
329 sc = device_get_softc(bridge_dev);
330 dev_priv = sc->drm_dev->dev_private;
332 intel_i2c_reset(sc->drm_dev);
333 intel_i2c_quirk_set(dev_priv, true);
334 IICBB_SETSDA(bridge_dev, 1);
335 IICBB_SETSCL(bridge_dev, 1);
336 DELAY(I2C_RISEFALL_TIME);
338 for (i = 0; i < nmsgs - 1; i++) {
339 /* force use of repeated start instead of default stop+start */
340 msgs[i].flags |= IIC_M_NOSTOP;
342 ret = iicbus_transfer(idev, msgs, nmsgs);
343 IICBB_SETSDA(bridge_dev, 1);
344 IICBB_SETSCL(bridge_dev, 1);
345 intel_i2c_quirk_set(dev_priv, false);
351 gmbus_wait_hw_status(struct drm_i915_private *dev_priv,
359 if (!HAS_GMBUS_IRQ(dev_priv->dev))
362 /* Important: The hw handles only the first bit, so set only one! Since
363 * we also need to check for NAKs besides the hw ready/idle signal, we
364 * need to wake up periodically and check that ourselves. */
365 I915_WRITE(GMBUS4, gmbus4_irq_en);
367 for (i = 0; i < msecs_to_jiffies_timeout(50); i++) {
368 prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait,
369 TASK_UNINTERRUPTIBLE);
371 gmbus2 = I915_READ_NOTRACE(GMBUS2);
372 if (gmbus2 & (GMBUS_SATOER | gmbus2_status))
377 finish_wait(&dev_priv->gmbus_wait_queue, &wait);
379 I915_WRITE(GMBUS4, 0);
381 if (gmbus2 & GMBUS_SATOER)
383 if (gmbus2 & gmbus2_status)
389 gmbus_wait_idle(struct drm_i915_private *dev_priv)
393 #define C ((I915_READ_NOTRACE(GMBUS2) & GMBUS_ACTIVE) == 0)
395 if (!HAS_GMBUS_IRQ(dev_priv->dev))
396 return wait_for(C, 10);
398 /* Important: The hw handles only the first bit, so set only one! */
399 I915_WRITE(GMBUS4, GMBUS_IDLE_EN);
401 ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
402 msecs_to_jiffies_timeout(10));
404 I915_WRITE(GMBUS4, 0);
414 gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
415 unsigned short addr, u8 *buf, unsigned int len,
421 (len << GMBUS_BYTE_COUNT_SHIFT) |
422 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
423 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
428 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
433 val = I915_READ(GMBUS3);
437 } while (--len && ++loop < 4);
444 gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
448 unsigned int rx_size = msg->len;
453 len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
455 ret = gmbus_xfer_read_chunk(dev_priv, msg->slave >> 1,
456 buf, len, gmbus1_index);
462 } while (rx_size != 0);
468 gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
469 unsigned short addr, u8 *buf, unsigned int len)
471 unsigned int chunk_size = len;
475 while (len && loop < 4) {
476 val |= *buf++ << (8 * loop++);
480 I915_WRITE(GMBUS3, val);
483 (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
484 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
485 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
491 val |= *buf++ << (8 * loop);
492 } while (--len && ++loop < 4);
494 I915_WRITE(GMBUS3, val);
496 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
506 gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
509 unsigned int tx_size = msg->len;
514 len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
516 ret = gmbus_xfer_write_chunk(dev_priv, msg->slave >> 1, buf, len);
522 } while (tx_size != 0);
528 * The gmbus controller can combine a 1 or 2 byte write with a read that
529 * immediately follows it by using an "INDEX" cycle.
532 gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
534 return (i + 1 < num &&
535 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
536 (msgs[i + 1].flags & I2C_M_RD));
540 gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
542 u32 gmbus1_index = 0;
546 if (msgs[0].len == 2)
547 gmbus5 = GMBUS_2BYTE_INDEX_EN |
548 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
549 if (msgs[0].len == 1)
550 gmbus1_index = GMBUS_CYCLE_INDEX |
551 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
553 /* GMBUS5 holds 16-bit index */
555 I915_WRITE(GMBUS5, gmbus5);
557 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
559 /* Clear GMBUS5 after each index transfer */
561 I915_WRITE(GMBUS5, 0);
567 gmbus_xfer(struct i2c_adapter *adapter,
568 struct i2c_msg *msgs,
571 struct intel_iic_softc *sc;
572 struct drm_i915_private *dev_priv;
573 int i = 0, inc, try = 0;
577 sc = device_get_softc(adapter);
578 dev_priv = sc->drm_dev->dev_private;
579 unit = device_get_unit(adapter);
581 intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS);
582 mutex_lock(&dev_priv->gmbus_mutex);
584 if (sc->force_bit_dev) {
585 ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num);
590 I915_WRITE(GMBUS0, sc->reg0);
592 for (; i < num; i += inc) {
594 if (gmbus_is_index_read(msgs, i, num)) {
595 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
596 inc = 2; /* an index read is two msgs */
597 } else if (msgs[i].flags & I2C_M_RD) {
598 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
600 ret = gmbus_xfer_write(dev_priv, &msgs[i]);
603 if (ret == -ETIMEDOUT)
608 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE,
616 /* Generate a STOP condition on the bus. Note that gmbus can't generata
617 * a STOP on the very first cycle. To simplify the code we
618 * unconditionally generate the STOP condition with an additional gmbus
620 I915_WRITE(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
622 /* Mark the GMBUS interface as disabled after waiting for idle.
623 * We will re-enable it at the start of the next xfer,
624 * till then let it sleep.
626 if (gmbus_wait_idle(dev_priv)) {
627 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
631 I915_WRITE(GMBUS0, 0);
633 goto timeout; /* XXX: should be out */
637 * Wait for bus to IDLE before clearing NAK.
638 * If we clear the NAK while bus is still active, then it will stay
639 * active and the next transaction may fail.
641 * If no ACK is received during the address phase of a transaction, the
642 * adapter must report -ENXIO. It is not clear what to return if no ACK
643 * is received at other times. But we have to be careful to not return
644 * spurious -ENXIO because that will prevent i2c and drm edid functions
645 * from retrying. So return -ENXIO only when gmbus properly quiescents -
646 * timing out seems to happen when there _is_ a ddc chip present, but
647 * it's slow responding and only answers on the 2nd retry.
650 if (gmbus_wait_idle(dev_priv)) {
651 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
656 /* Toggle the Software Clear Interrupt bit. This has the effect
657 * of resetting the GMBUS controller and so clearing the
658 * BUS_ERROR raised by the slave's NAK.
660 I915_WRITE(GMBUS1, GMBUS_SW_CLR_INT);
661 I915_WRITE(GMBUS1, 0);
662 I915_WRITE(GMBUS0, 0);
664 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
665 sc->name, msgs[i].slave,
666 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
669 * Passive adapters sometimes NAK the first probe. Retry the first
670 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm
671 * has retries internally. See also the retry loop in
672 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO.
674 if (ret == -ENXIO && i == 0 && try++ == 0) {
675 DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n",
683 DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
684 sc->name, sc->reg0 & 0xff);
685 I915_WRITE(GMBUS0, 0);
687 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
688 sc->force_bit_dev = true;
689 ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num);
692 mutex_unlock(&dev_priv->gmbus_mutex);
694 intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS);
699 struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
702 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
705 return dev_priv->gmbus[pin];
709 intel_gmbus_set_speed(device_t idev, int speed)
711 struct intel_iic_softc *sc;
713 sc = device_get_softc(device_get_parent(idev));
715 sc->reg0 = (sc->reg0 & ~(0x3 << 8)) | speed;
719 intel_gmbus_force_bit(device_t idev, bool force_bit)
721 struct intel_iic_softc *sc;
723 sc = device_get_softc(device_get_parent(idev));
724 sc->force_bit_dev += force_bit ? 1 : -1;
725 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
726 force_bit ? "en" : "dis", sc->name,
731 intel_gmbus_probe(device_t dev)
734 return (BUS_PROBE_SPECIFIC);
738 intel_gmbus_attach(device_t idev)
740 struct drm_i915_private *dev_priv;
741 struct intel_iic_softc *sc;
744 sc = device_get_softc(idev);
745 sc->drm_dev = device_get_softc(device_get_parent(idev));
746 dev_priv = sc->drm_dev->dev_private;
747 pin = device_get_unit(idev);
749 ksnprintf(sc->name, sizeof(sc->name), "gmbus bus %s", gpio_names[pin]);
750 device_set_desc(idev, sc->name);
752 /* By default use a conservative clock rate */
753 sc->reg0 = pin | GMBUS_RATE_100KHZ;
755 /* XXX force bit banging until GMBUS is fully debugged */
756 if (IS_GEN2(sc->drm_dev)) {
757 sc->force_bit_dev = true;
760 /* add bus interface device */
761 sc->iic_dev = device_add_child(idev, "iicbus", -1);
762 if (sc->iic_dev == NULL)
764 device_quiet(sc->iic_dev);
765 bus_generic_attach(idev);
771 intel_gmbus_detach(device_t idev)
773 struct intel_iic_softc *sc;
774 struct drm_i915_private *dev_priv;
778 sc = device_get_softc(idev);
779 u = device_get_unit(idev);
780 dev_priv = sc->drm_dev->dev_private;
783 bus_generic_detach(idev);
785 device_delete_child(idev, child);
791 intel_iicbb_probe(device_t dev)
794 return (BUS_PROBE_DEFAULT);
798 intel_iicbb_detach(device_t idev)
800 struct intel_iic_softc *sc;
803 sc = device_get_softc(idev);
805 bus_generic_detach(idev);
807 device_delete_child(idev, child);
811 static device_method_t intel_gmbus_methods[] = {
812 DEVMETHOD(device_probe, intel_gmbus_probe),
813 DEVMETHOD(device_attach, intel_gmbus_attach),
814 DEVMETHOD(device_detach, intel_gmbus_detach),
815 DEVMETHOD(iicbus_reset, intel_iicbus_reset),
816 DEVMETHOD(iicbus_transfer, gmbus_xfer),
819 static driver_t intel_gmbus_driver = {
822 sizeof(struct intel_iic_softc)
824 static devclass_t intel_gmbus_devclass;
825 DRIVER_MODULE_ORDERED(intel_gmbus, drm, intel_gmbus_driver,
826 intel_gmbus_devclass, NULL, NULL, SI_ORDER_FIRST);
827 DRIVER_MODULE(iicbus, intel_gmbus, iicbus_driver, iicbus_devclass, NULL, NULL);
829 static device_method_t intel_iicbb_methods[] = {
830 DEVMETHOD(device_probe, intel_iicbb_probe),
831 DEVMETHOD(device_attach, intel_gpio_setup),
832 DEVMETHOD(device_detach, intel_iicbb_detach),
834 DEVMETHOD(bus_add_child, bus_generic_add_child),
835 DEVMETHOD(bus_print_child, bus_generic_print_child),
837 DEVMETHOD(iicbb_callback, iicbus_null_callback),
838 DEVMETHOD(iicbb_reset, intel_iicbus_reset),
839 DEVMETHOD(iicbb_setsda, set_data),
840 DEVMETHOD(iicbb_setscl, set_clock),
841 DEVMETHOD(iicbb_getsda, get_data),
842 DEVMETHOD(iicbb_getscl, get_clock),
845 static driver_t intel_iicbb_driver = {
848 sizeof(struct intel_iic_softc)
850 static devclass_t intel_iicbb_devclass;
851 DRIVER_MODULE_ORDERED(intel_iicbb, drm, intel_iicbb_driver,
852 intel_iicbb_devclass, NULL, NULL, SI_ORDER_FIRST);
853 DRIVER_MODULE(iicbb, intel_iicbb, iicbb_driver, iicbb_devclass, NULL, NULL);
855 static void intel_teardown_gmbus_m(struct drm_device *dev, int m);
858 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
861 int intel_setup_gmbus(struct drm_device *dev)
863 struct drm_i915_private *dev_priv = dev->dev_private;
868 if (HAS_PCH_NOP(dev))
870 else if (HAS_PCH_SPLIT(dev))
871 dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
872 else if (IS_VALLEYVIEW(dev))
873 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
875 dev_priv->gpio_mmio_base = 0;
877 lockinit(&dev_priv->gmbus_mutex, "gmbus", 0, LK_CANRECURSE);
878 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
880 dev_priv->gmbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS,
881 M_DRM, M_WAITOK | M_ZERO);
882 dev_priv->bbbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS,
883 M_DRM, M_WAITOK | M_ZERO);
884 dev_priv->gmbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS,
885 M_DRM, M_WAITOK | M_ZERO);
886 dev_priv->bbbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS,
887 M_DRM, M_WAITOK | M_ZERO);
889 for (pin = 0; pin < GMBUS_NUM_PINS; pin++) {
890 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
894 * Initialized bbbus_bridge before gmbus_bridge, since
895 * gmbus may decide to force quirk transfer in the
898 dev_priv->bbbus_bridge[pin] = device_add_child(dev->dev->bsddev,
900 if (dev_priv->bbbus_bridge[pin] == NULL) {
901 DRM_ERROR("bbbus bridge %d creation failed\n", pin);
905 device_quiet(dev_priv->bbbus_bridge[pin]);
906 ret = device_probe_and_attach(dev_priv->bbbus_bridge[pin]);
908 DRM_ERROR("bbbus bridge %d attach failed, %d\n", pin, ret);
912 iic_dev = device_find_child(dev_priv->bbbus_bridge[pin], "iicbb",
914 if (iic_dev == NULL) {
915 DRM_ERROR("bbbus bridge doesn't have iicbb child\n");
918 iic_dev = device_find_child(iic_dev, "iicbus", -1);
919 if (iic_dev == NULL) {
921 "bbbus bridge doesn't have iicbus grandchild\n");
925 dev_priv->bbbus[pin] = iic_dev;
927 dev_priv->gmbus_bridge[pin] = device_add_child(dev->dev->bsddev,
929 if (dev_priv->gmbus_bridge[pin] == NULL) {
930 DRM_ERROR("gmbus bridge %d creation failed\n", pin);
934 device_quiet(dev_priv->gmbus_bridge[pin]);
935 ret = device_probe_and_attach(dev_priv->gmbus_bridge[pin]);
937 DRM_ERROR("gmbus bridge %d attach failed, %d\n", pin,
943 iic_dev = device_find_child(dev_priv->gmbus_bridge[pin],
945 if (iic_dev == NULL) {
946 DRM_ERROR("gmbus bridge doesn't have iicbus child\n");
949 dev_priv->gmbus[pin] = iic_dev;
951 intel_i2c_reset(dev);
957 intel_teardown_gmbus_m(dev, pin);
962 intel_teardown_gmbus_m(struct drm_device *dev, int m)
964 struct drm_i915_private *dev_priv;
966 dev_priv = dev->dev_private;
968 kfree(dev_priv->gmbus);
969 dev_priv->gmbus = NULL;
970 kfree(dev_priv->bbbus);
971 dev_priv->bbbus = NULL;
972 kfree(dev_priv->gmbus_bridge);
973 dev_priv->gmbus_bridge = NULL;
974 kfree(dev_priv->bbbus_bridge);
975 dev_priv->bbbus_bridge = NULL;
976 lockuninit(&dev_priv->gmbus_mutex);
980 intel_teardown_gmbus(struct drm_device *dev)
984 intel_teardown_gmbus_m(dev, GMBUS_NUM_PINS);