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"
82 static const struct gmbus_port gmbus_ports[] = {
91 /* Intel GPIO access functions */
93 #define I2C_RISEFALL_TIME 10
95 static int get_disp_clk_div(struct drm_i915_private *dev_priv,
101 reg_val = I915_READ(CZCLK_CDCLK_FREQ_RATIO);
105 ((reg_val & CDCLK_FREQ_MASK) >> CDCLK_FREQ_SHIFT) + 1;
107 clk_ratio = (reg_val & CZCLK_FREQ_MASK) + 1;
112 static void gmbus_set_freq(struct drm_i915_private *dev_priv)
114 int vco, gmbus_freq = 0, cdclk_div;
116 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
118 vco = valleyview_get_vco(dev_priv);
120 /* Get the CDCLK divide ratio */
121 cdclk_div = get_disp_clk_div(dev_priv, CDCLK);
124 * Program the gmbus_freq based on the cdclk frequency.
125 * BSpec erroneously claims we should aim for 4MHz, but
126 * in fact 1MHz is the correct frequency.
129 gmbus_freq = (vco << 1) / cdclk_div;
131 if (WARN_ON(gmbus_freq == 0))
134 I915_WRITE(GMBUSFREQ_VLV, gmbus_freq);
138 intel_i2c_reset(struct drm_device *dev)
140 struct drm_i915_private *dev_priv = dev->dev_private;
143 * In BIOS-less system, program the correct gmbus frequency
144 * before reading edid.
146 if (IS_VALLEYVIEW(dev))
147 gmbus_set_freq(dev_priv);
149 I915_WRITE(dev_priv->gpio_mmio_base + GMBUS0, 0);
150 I915_WRITE(dev_priv->gpio_mmio_base + GMBUS4, 0);
153 static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
157 /* When using bit bashing for I2C, this bit needs to be set to 1 */
158 if (!IS_PINEVIEW(dev_priv->dev))
161 val = I915_READ(DSPCLK_GATE_D);
163 val |= DPCUNIT_CLOCK_GATE_DISABLE;
165 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
166 I915_WRITE(DSPCLK_GATE_D, val);
169 static u32 get_reserved(device_t idev)
171 struct intel_iic_softc *sc = device_get_softc(idev);
172 struct drm_device *dev = sc->drm_dev;
173 struct drm_i915_private *dev_priv;
176 dev_priv = dev->dev_private;
178 /* On most chips, these bits must be preserved in software. */
179 if (!IS_I830(dev) && !IS_845G(dev))
180 reserved = I915_READ_NOTRACE(sc->reg) &
181 (GPIO_DATA_PULLUP_DISABLE |
182 GPIO_CLOCK_PULLUP_DISABLE);
187 static int get_clock(device_t idev)
189 struct intel_iic_softc *sc;
190 struct drm_i915_private *dev_priv;
193 sc = device_get_softc(idev);
194 dev_priv = sc->drm_dev->dev_private;
196 reserved = get_reserved(idev);
198 I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_CLOCK_DIR_MASK);
199 I915_WRITE_NOTRACE(sc->reg, reserved);
200 return ((I915_READ_NOTRACE(sc->reg) & GPIO_CLOCK_VAL_IN) != 0);
203 static int get_data(device_t idev)
205 struct intel_iic_softc *sc;
206 struct drm_i915_private *dev_priv;
209 sc = device_get_softc(idev);
210 dev_priv = sc->drm_dev->dev_private;
212 reserved = get_reserved(idev);
214 I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_DATA_DIR_MASK);
215 I915_WRITE_NOTRACE(sc->reg, reserved);
216 return ((I915_READ_NOTRACE(sc->reg) & GPIO_DATA_VAL_IN) != 0);
220 intel_iicbus_reset(device_t idev, u_char speed, u_char addr, u_char *oldaddr)
222 struct intel_iic_softc *sc;
223 struct drm_device *dev;
225 sc = device_get_softc(idev);
228 intel_i2c_reset(dev);
232 static void set_clock(device_t idev, int val)
234 struct intel_iic_softc *sc;
235 struct drm_i915_private *dev_priv;
236 u32 clock_bits, reserved;
238 sc = device_get_softc(idev);
239 dev_priv = sc->drm_dev->dev_private;
241 reserved = get_reserved(idev);
243 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
245 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
248 I915_WRITE_NOTRACE(sc->reg, reserved | clock_bits);
249 POSTING_READ(sc->reg);
252 static void set_data(device_t idev, int val)
254 struct intel_iic_softc *sc;
255 struct drm_i915_private *dev_priv;
259 sc = device_get_softc(idev);
260 dev_priv = sc->drm_dev->dev_private;
262 reserved = get_reserved(idev);
264 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
266 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
269 I915_WRITE_NOTRACE(sc->reg, reserved | data_bits);
270 POSTING_READ(sc->reg);
273 static const char *gpio_names[GMBUS_NUM_PORTS] = {
283 intel_gpio_setup(device_t idev)
285 static const int map_pin_to_reg[] = {
296 struct intel_iic_softc *sc;
297 struct drm_i915_private *dev_priv;
300 sc = device_get_softc(idev);
301 sc->drm_dev = device_get_softc(device_get_parent(idev));
302 dev_priv = sc->drm_dev->dev_private;
303 pin = device_get_unit(idev);
305 ksnprintf(sc->name, sizeof(sc->name), "i915 iicbb %s", gpio_names[pin]);
306 device_set_desc(idev, sc->name);
308 sc->reg0 = (pin + 1) | GMBUS_RATE_100KHZ;
309 sc->reg = map_pin_to_reg[pin + 1];
310 if (HAS_PCH_SPLIT(dev_priv->dev))
311 sc->reg += PCH_GPIOA - GPIOA;
313 /* add generic bit-banging code */
314 sc->iic_dev = device_add_child(idev, "iicbb", -1);
315 if (sc->iic_dev == NULL)
317 device_quiet(sc->iic_dev);
318 bus_generic_attach(idev);
324 intel_i2c_quirk_xfer(device_t idev, struct iic_msg *msgs, int nmsgs)
327 struct intel_iic_softc *sc;
328 struct drm_i915_private *dev_priv;
332 bridge_dev = device_get_parent(device_get_parent(idev));
333 sc = device_get_softc(bridge_dev);
334 dev_priv = sc->drm_dev->dev_private;
336 intel_i2c_reset(sc->drm_dev);
337 intel_i2c_quirk_set(dev_priv, true);
338 IICBB_SETSDA(bridge_dev, 1);
339 IICBB_SETSCL(bridge_dev, 1);
340 DELAY(I2C_RISEFALL_TIME);
342 for (i = 0; i < nmsgs - 1; i++) {
343 /* force use of repeated start instead of default stop+start */
344 msgs[i].flags |= IIC_M_NOSTOP;
346 ret = iicbus_transfer(idev, msgs, nmsgs);
347 IICBB_SETSDA(bridge_dev, 1);
348 IICBB_SETSCL(bridge_dev, 1);
349 intel_i2c_quirk_set(dev_priv, false);
355 gmbus_wait_hw_status(struct drm_i915_private *dev_priv,
360 int reg_offset = dev_priv->gpio_mmio_base;
364 if (!HAS_GMBUS_IRQ(dev_priv->dev))
367 /* Important: The hw handles only the first bit, so set only one! Since
368 * we also need to check for NAKs besides the hw ready/idle signal, we
369 * need to wake up periodically and check that ourselves. */
370 I915_WRITE(GMBUS4 + reg_offset, gmbus4_irq_en);
372 for (i = 0; i < msecs_to_jiffies_timeout(50); i++) {
373 prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait,
374 TASK_UNINTERRUPTIBLE);
376 gmbus2 = I915_READ_NOTRACE(GMBUS2 + reg_offset);
377 if (gmbus2 & (GMBUS_SATOER | gmbus2_status))
382 finish_wait(&dev_priv->gmbus_wait_queue, &wait);
384 I915_WRITE(GMBUS4 + reg_offset, 0);
386 if (gmbus2 & GMBUS_SATOER)
388 if (gmbus2 & gmbus2_status)
394 gmbus_wait_idle(struct drm_i915_private *dev_priv)
397 int reg_offset = dev_priv->gpio_mmio_base;
399 #define C ((I915_READ_NOTRACE(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0)
401 if (!HAS_GMBUS_IRQ(dev_priv->dev))
402 return wait_for(C, 10);
404 /* Important: The hw handles only the first bit, so set only one! */
405 I915_WRITE(GMBUS4 + reg_offset, GMBUS_IDLE_EN);
407 ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
408 msecs_to_jiffies_timeout(10));
410 I915_WRITE(GMBUS4 + reg_offset, 0);
420 gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
423 int reg_offset = dev_priv->gpio_mmio_base;
427 I915_WRITE(GMBUS1 + reg_offset,
430 (len << GMBUS_BYTE_COUNT_SHIFT) |
431 (msg->slave << (GMBUS_SLAVE_ADDR_SHIFT - 1)) |
432 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
437 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
442 val = I915_READ(GMBUS3 + reg_offset);
446 } while (--len && ++loop < 4);
453 gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
455 int reg_offset = dev_priv->gpio_mmio_base;
461 while (len && loop < 4) {
462 val |= *buf++ << (8 * loop++);
466 I915_WRITE(GMBUS3 + reg_offset, val);
467 I915_WRITE(GMBUS1 + reg_offset,
469 (msg->len << GMBUS_BYTE_COUNT_SHIFT) |
470 (msg->slave << (GMBUS_SLAVE_ADDR_SHIFT - 1)) |
471 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
477 val |= *buf++ << (8 * loop);
478 } while (--len && ++loop < 4);
480 I915_WRITE(GMBUS3 + reg_offset, val);
482 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
491 * The gmbus controller can combine a 1 or 2 byte write with a read that
492 * immediately follows it by using an "INDEX" cycle.
495 gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
497 return (i + 1 < num &&
498 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
499 (msgs[i + 1].flags & I2C_M_RD));
503 gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
505 int reg_offset = dev_priv->gpio_mmio_base;
506 u32 gmbus1_index = 0;
510 if (msgs[0].len == 2)
511 gmbus5 = GMBUS_2BYTE_INDEX_EN |
512 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
513 if (msgs[0].len == 1)
514 gmbus1_index = GMBUS_CYCLE_INDEX |
515 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
517 /* GMBUS5 holds 16-bit index */
519 I915_WRITE(GMBUS5 + reg_offset, gmbus5);
521 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
523 /* Clear GMBUS5 after each index transfer */
525 I915_WRITE(GMBUS5 + reg_offset, 0);
531 gmbus_xfer(struct device *adapter,
532 struct i2c_msg *msgs,
535 struct intel_iic_softc *sc;
536 struct drm_i915_private *dev_priv;
537 int i, reg_offset, unit;
540 sc = device_get_softc(adapter);
541 dev_priv = sc->drm_dev->dev_private;
542 unit = device_get_unit(adapter);
544 mutex_lock(&dev_priv->gmbus_mutex);
546 if (sc->force_bit_dev) {
547 ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num);
551 reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0;
553 I915_WRITE(GMBUS0 + reg_offset, sc->reg0);
555 for (i = 0; i < num; i++) {
556 if (gmbus_is_index_read(msgs, i, num)) {
557 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
558 i += 1; /* set i to the index of the read xfer */
559 } else if (msgs[i].flags & I2C_M_RD) {
560 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
562 ret = gmbus_xfer_write(dev_priv, &msgs[i]);
565 if (ret == -ETIMEDOUT)
570 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE,
578 /* Generate a STOP condition on the bus. Note that gmbus can't generata
579 * a STOP on the very first cycle. To simplify the code we
580 * unconditionally generate the STOP condition with an additional gmbus
582 I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
584 /* Mark the GMBUS interface as disabled after waiting for idle.
585 * We will re-enable it at the start of the next xfer,
586 * till then let it sleep.
588 if (gmbus_wait_idle(dev_priv)) {
589 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
593 I915_WRITE(GMBUS0 + reg_offset, 0);
595 goto timeout; /* XXX: should be out */
599 * Wait for bus to IDLE before clearing NAK.
600 * If we clear the NAK while bus is still active, then it will stay
601 * active and the next transaction may fail.
603 * If no ACK is received during the address phase of a transaction, the
604 * adapter must report -ENXIO. It is not clear what to return if no ACK
605 * is received at other times. But we have to be careful to not return
606 * spurious -ENXIO because that will prevent i2c and drm edid functions
607 * from retrying. So return -ENXIO only when gmbus properly quiescents -
608 * timing out seems to happen when there _is_ a ddc chip present, but
609 * it's slow responding and only answers on the 2nd retry.
612 if (gmbus_wait_idle(dev_priv)) {
613 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
618 /* Toggle the Software Clear Interrupt bit. This has the effect
619 * of resetting the GMBUS controller and so clearing the
620 * BUS_ERROR raised by the slave's NAK.
622 I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
623 I915_WRITE(GMBUS1 + reg_offset, 0);
624 I915_WRITE(GMBUS0 + reg_offset, 0);
626 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
627 sc->name, msgs[i].slave,
628 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
633 DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
634 sc->name, sc->reg0 & 0xff);
635 I915_WRITE(GMBUS0 + reg_offset, 0);
637 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
638 sc->force_bit_dev = true;
639 ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num);
642 mutex_unlock(&dev_priv->gmbus_mutex);
646 struct device *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
649 WARN_ON(!intel_gmbus_is_port_valid(port));
650 /* -1 to map pin pair to gmbus index */
651 return (intel_gmbus_is_port_valid(port)) ?
652 dev_priv->gmbus[port-1] : NULL;
656 intel_gmbus_set_speed(device_t idev, int speed)
658 struct intel_iic_softc *sc;
660 sc = device_get_softc(device_get_parent(idev));
662 sc->reg0 = (sc->reg0 & ~(0x3 << 8)) | speed;
666 intel_gmbus_force_bit(device_t idev, bool force_bit)
668 struct intel_iic_softc *sc;
670 sc = device_get_softc(device_get_parent(idev));
671 sc->force_bit_dev += force_bit ? 1 : -1;
672 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
673 force_bit ? "en" : "dis", sc->name,
678 intel_gmbus_probe(device_t dev)
681 return (BUS_PROBE_SPECIFIC);
685 intel_gmbus_attach(device_t idev)
687 struct drm_i915_private *dev_priv;
688 struct intel_iic_softc *sc;
691 sc = device_get_softc(idev);
692 sc->drm_dev = device_get_softc(device_get_parent(idev));
693 dev_priv = sc->drm_dev->dev_private;
694 pin = device_get_unit(idev);
696 ksnprintf(sc->name, sizeof(sc->name), "gmbus bus %s", gpio_names[pin]);
697 device_set_desc(idev, sc->name);
699 /* By default use a conservative clock rate */
700 sc->reg0 = (pin + 1) | GMBUS_RATE_100KHZ;
702 /* XXX force bit banging until GMBUS is fully debugged */
703 if (IS_GEN2(sc->drm_dev)) {
704 sc->force_bit_dev = true;
707 /* add bus interface device */
708 sc->iic_dev = device_add_child(idev, "iicbus", -1);
709 if (sc->iic_dev == NULL)
711 device_quiet(sc->iic_dev);
712 bus_generic_attach(idev);
718 intel_gmbus_detach(device_t idev)
720 struct intel_iic_softc *sc;
721 struct drm_i915_private *dev_priv;
725 sc = device_get_softc(idev);
726 u = device_get_unit(idev);
727 dev_priv = sc->drm_dev->dev_private;
730 bus_generic_detach(idev);
732 device_delete_child(idev, child);
738 intel_iicbb_probe(device_t dev)
741 return (BUS_PROBE_DEFAULT);
745 intel_iicbb_detach(device_t idev)
747 struct intel_iic_softc *sc;
750 sc = device_get_softc(idev);
752 bus_generic_detach(idev);
754 device_delete_child(idev, child);
758 static device_method_t intel_gmbus_methods[] = {
759 DEVMETHOD(device_probe, intel_gmbus_probe),
760 DEVMETHOD(device_attach, intel_gmbus_attach),
761 DEVMETHOD(device_detach, intel_gmbus_detach),
762 DEVMETHOD(iicbus_reset, intel_iicbus_reset),
763 DEVMETHOD(iicbus_transfer, gmbus_xfer),
766 static driver_t intel_gmbus_driver = {
769 sizeof(struct intel_iic_softc)
771 static devclass_t intel_gmbus_devclass;
772 DRIVER_MODULE_ORDERED(intel_gmbus, drm, intel_gmbus_driver,
773 intel_gmbus_devclass, 0, 0, SI_ORDER_FIRST);
774 DRIVER_MODULE(iicbus, intel_gmbus, iicbus_driver, iicbus_devclass, NULL, NULL);
776 static device_method_t intel_iicbb_methods[] = {
777 DEVMETHOD(device_probe, intel_iicbb_probe),
778 DEVMETHOD(device_attach, intel_gpio_setup),
779 DEVMETHOD(device_detach, intel_iicbb_detach),
781 DEVMETHOD(bus_add_child, bus_generic_add_child),
782 DEVMETHOD(bus_print_child, bus_generic_print_child),
784 DEVMETHOD(iicbb_callback, iicbus_null_callback),
785 DEVMETHOD(iicbb_reset, intel_iicbus_reset),
786 DEVMETHOD(iicbb_setsda, set_data),
787 DEVMETHOD(iicbb_setscl, set_clock),
788 DEVMETHOD(iicbb_getsda, get_data),
789 DEVMETHOD(iicbb_getscl, get_clock),
792 static driver_t intel_iicbb_driver = {
795 sizeof(struct intel_iic_softc)
797 static devclass_t intel_iicbb_devclass;
798 DRIVER_MODULE_ORDERED(intel_iicbb, drm, intel_iicbb_driver,
799 intel_iicbb_devclass, 0, 0, SI_ORDER_FIRST);
800 DRIVER_MODULE(iicbb, intel_iicbb, iicbb_driver, iicbb_devclass, NULL, NULL);
802 static void intel_teardown_gmbus_m(struct drm_device *dev, int m);
805 intel_setup_gmbus(struct drm_device *dev)
807 struct drm_i915_private *dev_priv = dev->dev_private;
811 if (HAS_PCH_NOP(dev))
813 else if (HAS_PCH_SPLIT(dev))
814 dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
815 else if (IS_VALLEYVIEW(dev))
816 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
818 dev_priv->gpio_mmio_base = 0;
820 lockinit(&dev_priv->gmbus_mutex, "gmbus", 0, LK_CANRECURSE);
821 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
823 dev_priv->gmbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
824 M_DRM, M_WAITOK | M_ZERO);
825 dev_priv->bbbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
826 M_DRM, M_WAITOK | M_ZERO);
827 dev_priv->gmbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
828 M_DRM, M_WAITOK | M_ZERO);
829 dev_priv->bbbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
830 M_DRM, M_WAITOK | M_ZERO);
832 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
834 * Initialized bbbus_bridge before gmbus_bridge, since
835 * gmbus may decide to force quirk transfer in the
838 dev_priv->bbbus_bridge[i] = device_add_child(dev->dev,
840 if (dev_priv->bbbus_bridge[i] == NULL) {
841 DRM_ERROR("bbbus bridge %d creation failed\n", i);
845 device_quiet(dev_priv->bbbus_bridge[i]);
846 ret = device_probe_and_attach(dev_priv->bbbus_bridge[i]);
848 DRM_ERROR("bbbus bridge %d attach failed, %d\n", i,
853 iic_dev = device_find_child(dev_priv->bbbus_bridge[i], "iicbb",
855 if (iic_dev == NULL) {
856 DRM_ERROR("bbbus bridge doesn't have iicbb child\n");
859 iic_dev = device_find_child(iic_dev, "iicbus", -1);
860 if (iic_dev == NULL) {
862 "bbbus bridge doesn't have iicbus grandchild\n");
866 dev_priv->bbbus[i] = iic_dev;
868 dev_priv->gmbus_bridge[i] = device_add_child(dev->dev,
870 if (dev_priv->gmbus_bridge[i] == NULL) {
871 DRM_ERROR("gmbus bridge %d creation failed\n", i);
875 device_quiet(dev_priv->gmbus_bridge[i]);
876 ret = device_probe_and_attach(dev_priv->gmbus_bridge[i]);
878 DRM_ERROR("gmbus bridge %d attach failed, %d\n", i,
884 iic_dev = device_find_child(dev_priv->gmbus_bridge[i],
886 if (iic_dev == NULL) {
887 DRM_ERROR("gmbus bridge doesn't have iicbus child\n");
890 dev_priv->gmbus[i] = iic_dev;
892 intel_i2c_reset(dev);
898 intel_teardown_gmbus_m(dev, i);
903 intel_teardown_gmbus_m(struct drm_device *dev, int m)
905 struct drm_i915_private *dev_priv;
907 dev_priv = dev->dev_private;
909 drm_free(dev_priv->gmbus, M_DRM);
910 dev_priv->gmbus = NULL;
911 drm_free(dev_priv->bbbus, M_DRM);
912 dev_priv->bbbus = NULL;
913 drm_free(dev_priv->gmbus_bridge, M_DRM);
914 dev_priv->gmbus_bridge = NULL;
915 drm_free(dev_priv->bbbus_bridge, M_DRM);
916 dev_priv->bbbus_bridge = NULL;
917 lockuninit(&dev_priv->gmbus_mutex);
921 intel_teardown_gmbus(struct drm_device *dev)
925 intel_teardown_gmbus_m(dev, GMBUS_NUM_PORTS);