Merge branch 'openssh'
[dragonfly.git] / sys / dev / drm / i915 / intel_i2c.c
1 /*
2  * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
3  * Copyright © 2006-2008,2010 Intel Corporation
4  *   Jesse Barnes <jesse.barnes@intel.com>
5  *
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:
12  *
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
15  * Software.
16  *
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.
24  *
25  * Authors:
26  *      Eric Anholt <eric@anholt.net>
27  *      Chris Wilson <chris@chris-wilson.co.uk>
28  *
29  * Copyright (c) 2011 The FreeBSD Foundation
30  * All rights reserved.
31  *
32  * This software was developed by Konstantin Belousov under sponsorship from
33  * the FreeBSD Foundation.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
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.
43  *
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
54  * SUCH DAMAGE.
55  */
56
57 #include <sys/mplock2.h>
58
59 #include <linux/i2c.h>
60 #include <linux/export.h>
61 #include <drm/drmP.h>
62 #include "intel_drv.h"
63 #include <drm/i915_drm.h>
64 #include "i915_drv.h"
65
66 #include <bus/iicbus/iic.h>
67 #include <bus/iicbus/iiconf.h>
68 #include <bus/iicbus/iicbus.h>
69 #include "iicbus_if.h"
70 #include "iicbb_if.h"
71
72 struct gmbus_port {
73         const char *name;
74         int reg;
75 };
76
77 static const struct gmbus_port gmbus_ports[] = {
78         { "ssc", GPIOB },
79         { "vga", GPIOA },
80         { "panel", GPIOC },
81         { "dpc", GPIOD },
82         { "dpb", GPIOE },
83         { "dpd", GPIOF },
84 };
85
86 /* Intel GPIO access functions */
87
88 #define I2C_RISEFALL_TIME 10
89
90 void
91 intel_i2c_reset(struct drm_device *dev)
92 {
93         struct drm_i915_private *dev_priv = dev->dev_private;
94         I915_WRITE(dev_priv->gpio_mmio_base + GMBUS0, 0);
95         I915_WRITE(dev_priv->gpio_mmio_base + GMBUS4, 0);
96 }
97
98 static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
99 {
100         u32 val;
101
102         /* When using bit bashing for I2C, this bit needs to be set to 1 */
103         if (!IS_PINEVIEW(dev_priv->dev))
104                 return;
105
106         val = I915_READ(DSPCLK_GATE_D);
107         if (enable)
108                 val |= DPCUNIT_CLOCK_GATE_DISABLE;
109         else
110                 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
111         I915_WRITE(DSPCLK_GATE_D, val);
112 }
113
114 static u32 get_reserved(device_t idev)
115 {
116         struct intel_iic_softc *sc = device_get_softc(idev);
117         struct drm_device *dev = sc->drm_dev;
118         struct drm_i915_private *dev_priv;
119         u32 reserved = 0;
120
121         dev_priv = dev->dev_private;
122
123         /* On most chips, these bits must be preserved in software. */
124         if (!IS_I830(dev) && !IS_845G(dev))
125                 reserved = I915_READ_NOTRACE(sc->reg) &
126                                              (GPIO_DATA_PULLUP_DISABLE |
127                                               GPIO_CLOCK_PULLUP_DISABLE);
128
129         return reserved;
130 }
131
132 static int get_clock(device_t idev)
133 {
134         struct intel_iic_softc *sc;
135         struct drm_i915_private *dev_priv;
136         u32 reserved;
137
138         sc = device_get_softc(idev);
139         dev_priv = sc->drm_dev->dev_private;
140
141         reserved = get_reserved(idev);
142
143         I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_CLOCK_DIR_MASK);
144         I915_WRITE_NOTRACE(sc->reg, reserved);
145         return ((I915_READ_NOTRACE(sc->reg) & GPIO_CLOCK_VAL_IN) != 0);
146 }
147
148 static int get_data(device_t idev)
149 {
150         struct intel_iic_softc *sc;
151         struct drm_i915_private *dev_priv;
152         u32 reserved;
153
154         sc = device_get_softc(idev);
155         dev_priv = sc->drm_dev->dev_private;
156
157         reserved = get_reserved(idev);
158
159         I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_DATA_DIR_MASK);
160         I915_WRITE_NOTRACE(sc->reg, reserved);
161         return ((I915_READ_NOTRACE(sc->reg) & GPIO_DATA_VAL_IN) != 0);
162 }
163
164 static int
165 intel_iicbus_reset(device_t idev, u_char speed, u_char addr, u_char *oldaddr)
166 {
167         struct intel_iic_softc *sc;
168         struct drm_device *dev;
169
170         sc = device_get_softc(idev);
171         dev = sc->drm_dev;
172
173         intel_i2c_reset(dev);
174         return (0);
175 }
176
177 static void set_clock(device_t idev, int val)
178 {
179         struct intel_iic_softc *sc;
180         struct drm_i915_private *dev_priv;
181         u32 clock_bits, reserved;
182
183         sc = device_get_softc(idev);
184         dev_priv = sc->drm_dev->dev_private;
185
186         reserved = get_reserved(idev);
187         if (val)
188                 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
189         else
190                 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
191                     GPIO_CLOCK_VAL_MASK;
192
193         I915_WRITE_NOTRACE(sc->reg, reserved | clock_bits);
194         POSTING_READ(sc->reg);
195 }
196
197 static void set_data(device_t idev, int val)
198 {
199         struct intel_iic_softc *sc;
200         struct drm_i915_private *dev_priv;
201         u32 reserved;
202         u32 data_bits;
203
204         sc = device_get_softc(idev);
205         dev_priv = sc->drm_dev->dev_private;
206
207         reserved = get_reserved(idev);
208         if (val)
209                 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
210         else
211                 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
212                     GPIO_DATA_VAL_MASK;
213
214         I915_WRITE_NOTRACE(sc->reg, reserved | data_bits);
215         POSTING_READ(sc->reg);
216 }
217
218 static const char *gpio_names[GMBUS_NUM_PORTS] = {
219         "ssc",
220         "vga",
221         "panel",
222         "dpc",
223         "dpb",
224         "dpd",
225 };
226
227 static int
228 intel_gpio_setup(device_t idev)
229 {
230         static const int map_pin_to_reg[] = {
231                 0,
232                 GPIOB,
233                 GPIOA,
234                 GPIOC,
235                 GPIOD,
236                 GPIOE,
237                 GPIOF,
238                 0
239         };
240
241         struct intel_iic_softc *sc;
242         struct drm_i915_private *dev_priv;
243         int pin;
244
245         sc = device_get_softc(idev);
246         sc->drm_dev = device_get_softc(device_get_parent(idev));
247         dev_priv = sc->drm_dev->dev_private;
248         pin = device_get_unit(idev);
249
250         ksnprintf(sc->name, sizeof(sc->name), "i915 iicbb %s", gpio_names[pin]);
251         device_set_desc(idev, sc->name);
252
253         sc->reg0 = (pin + 1) | GMBUS_RATE_100KHZ;
254         sc->reg = map_pin_to_reg[pin + 1];
255         if (HAS_PCH_SPLIT(dev_priv->dev))
256                 sc->reg += PCH_GPIOA - GPIOA;
257
258         /* add generic bit-banging code */
259         sc->iic_dev = device_add_child(idev, "iicbb", -1);
260         if (sc->iic_dev == NULL)
261                 return (ENXIO);
262         device_quiet(sc->iic_dev);
263         bus_generic_attach(idev);
264
265         return (0);
266 }
267
268 static int
269 intel_i2c_quirk_xfer(device_t idev, struct iic_msg *msgs, int nmsgs)
270 {
271         device_t bridge_dev;
272         struct intel_iic_softc *sc;
273         struct drm_i915_private *dev_priv;
274         int ret;
275         int i;
276
277         bridge_dev = device_get_parent(device_get_parent(idev));
278         sc = device_get_softc(bridge_dev);
279         dev_priv = sc->drm_dev->dev_private;
280
281         intel_i2c_reset(sc->drm_dev);
282         intel_i2c_quirk_set(dev_priv, true);
283         IICBB_SETSDA(bridge_dev, 1);
284         IICBB_SETSCL(bridge_dev, 1);
285         DELAY(I2C_RISEFALL_TIME);
286
287         for (i = 0; i < nmsgs - 1; i++) {
288                 /* force use of repeated start instead of default stop+start */
289                 msgs[i].flags |= IIC_M_NOSTOP;
290         }
291         ret = iicbus_transfer(idev, msgs, nmsgs);
292         IICBB_SETSDA(bridge_dev, 1);
293         IICBB_SETSCL(bridge_dev, 1);
294         intel_i2c_quirk_set(dev_priv, false);
295
296         return (ret);
297 }
298
299 /*
300  * gmbus on gen4 seems to be able to generate legacy interrupts even when in MSI
301  * mode. This results in spurious interrupt warnings if the legacy irq no. is
302  * shared with another device. The kernel then disables that interrupt source
303  * and so prevents the other device from working properly.
304  */
305 #define HAS_GMBUS_IRQ(dev) (INTEL_INFO(dev)->gen >= 5)
306 static int
307 gmbus_wait_hw_status(struct drm_i915_private *dev_priv,
308                      u32 gmbus2_status,
309                      u32 gmbus4_irq_en)
310 {
311         int i;
312         int reg_offset = dev_priv->gpio_mmio_base;
313         u32 gmbus2 = 0;
314         DEFINE_WAIT(wait);
315
316         if (!HAS_GMBUS_IRQ(dev_priv->dev))
317                 gmbus4_irq_en = 0;
318
319         /* Important: The hw handles only the first bit, so set only one! Since
320          * we also need to check for NAKs besides the hw ready/idle signal, we
321          * need to wake up periodically and check that ourselves. */
322         I915_WRITE(GMBUS4 + reg_offset, gmbus4_irq_en);
323
324         for (i = 0; i < msecs_to_jiffies(50) + 1; i++) {
325                 prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait,
326                                 TASK_UNINTERRUPTIBLE);
327
328                 gmbus2 = I915_READ_NOTRACE(GMBUS2 + reg_offset);
329                 if (gmbus2 & (GMBUS_SATOER | gmbus2_status))
330                         break;
331
332                 schedule_timeout(1);
333         }
334         finish_wait(&dev_priv->gmbus_wait_queue, &wait);
335
336         I915_WRITE(GMBUS4 + reg_offset, 0);
337
338         if (gmbus2 & GMBUS_SATOER)
339                 return -ENXIO;
340         if (gmbus2 & gmbus2_status)
341                 return 0;
342         return -ETIMEDOUT;
343 }
344
345 static int
346 gmbus_wait_idle(struct drm_i915_private *dev_priv)
347 {
348         int ret;
349         int reg_offset = dev_priv->gpio_mmio_base;
350
351 #define C ((I915_READ_NOTRACE(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0)
352
353         if (!HAS_GMBUS_IRQ(dev_priv->dev))
354                 return wait_for(C, 10);
355
356         /* Important: The hw handles only the first bit, so set only one! */
357         I915_WRITE(GMBUS4 + reg_offset, GMBUS_IDLE_EN);
358
359         ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C, 10);
360
361         I915_WRITE(GMBUS4 + reg_offset, 0);
362
363         if (ret)
364                 return 0;
365         else
366                 return -ETIMEDOUT;
367 #undef C
368 }
369
370 static int
371 gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
372                 u32 gmbus1_index)
373 {
374         int reg_offset = dev_priv->gpio_mmio_base;
375         u16 len = msg->len;
376         u8 *buf = msg->buf;
377
378         I915_WRITE(GMBUS1 + reg_offset,
379                    gmbus1_index |
380                    GMBUS_CYCLE_WAIT |
381                    (len << GMBUS_BYTE_COUNT_SHIFT) |
382                    (msg->slave << (GMBUS_SLAVE_ADDR_SHIFT - 1)) |
383                    GMBUS_SLAVE_READ | GMBUS_SW_RDY);
384         while (len) {
385                 int ret;
386                 u32 val, loop = 0;
387
388                 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
389                                            GMBUS_HW_RDY_EN);
390                 if (ret)
391                         return ret;
392
393                 val = I915_READ(GMBUS3 + reg_offset);
394                 do {
395                         *buf++ = val & 0xff;
396                         val >>= 8;
397                 } while (--len && ++loop < 4);
398         }
399
400         return 0;
401 }
402
403 static int
404 gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
405 {
406         int reg_offset = dev_priv->gpio_mmio_base;
407         u16 len = msg->len;
408         u8 *buf = msg->buf;
409         u32 val, loop;
410
411         val = loop = 0;
412         while (len && loop < 4) {
413                 val |= *buf++ << (8 * loop++);
414                 len -= 1;
415         }
416
417         I915_WRITE(GMBUS3 + reg_offset, val);
418         I915_WRITE(GMBUS1 + reg_offset,
419                    GMBUS_CYCLE_WAIT |
420                    (msg->len << GMBUS_BYTE_COUNT_SHIFT) |
421                    (msg->slave << (GMBUS_SLAVE_ADDR_SHIFT - 1)) |
422                    GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
423         while (len) {
424                 int ret;
425
426                 val = loop = 0;
427                 do {
428                         val |= *buf++ << (8 * loop);
429                 } while (--len && ++loop < 4);
430
431                 I915_WRITE(GMBUS3 + reg_offset, val);
432
433                 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
434                                            GMBUS_HW_RDY_EN);
435                 if (ret)
436                         return ret;
437         }
438         return 0;
439 }
440
441 /*
442  * The gmbus controller can combine a 1 or 2 byte write with a read that
443  * immediately follows it by using an "INDEX" cycle.
444  */
445 static bool
446 gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
447 {
448         return (i + 1 < num &&
449                 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
450                 (msgs[i + 1].flags & I2C_M_RD));
451 }
452
453 static int
454 gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
455 {
456         int reg_offset = dev_priv->gpio_mmio_base;
457         u32 gmbus1_index = 0;
458         u32 gmbus5 = 0;
459         int ret;
460
461         if (msgs[0].len == 2)
462                 gmbus5 = GMBUS_2BYTE_INDEX_EN |
463                          msgs[0].buf[1] | (msgs[0].buf[0] << 8);
464         if (msgs[0].len == 1)
465                 gmbus1_index = GMBUS_CYCLE_INDEX |
466                                (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
467
468         /* GMBUS5 holds 16-bit index */
469         if (gmbus5)
470                 I915_WRITE(GMBUS5 + reg_offset, gmbus5);
471
472         ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
473
474         /* Clear GMBUS5 after each index transfer */
475         if (gmbus5)
476                 I915_WRITE(GMBUS5 + reg_offset, 0);
477
478         return ret;
479 }
480
481 static int
482 gmbus_xfer(struct device *adapter,
483            struct i2c_msg *msgs,
484            int num)
485 {
486         struct intel_iic_softc *sc;
487         struct drm_i915_private *dev_priv;
488         int i, reg_offset, unit;
489         int ret = 0;
490
491         sc = device_get_softc(adapter);
492         dev_priv = sc->drm_dev->dev_private;
493         unit = device_get_unit(adapter);
494
495         mutex_lock(&dev_priv->gmbus_mutex);
496
497         if (sc->force_bit_dev) {
498                 ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num);
499                 goto out;
500         }
501
502         reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0;
503
504         I915_WRITE(GMBUS0 + reg_offset, sc->reg0);
505
506         for (i = 0; i < num; i++) {
507                 if (gmbus_is_index_read(msgs, i, num)) {
508                         ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
509                         i += 1;  /* set i to the index of the read xfer */
510                 } else if (msgs[i].flags & I2C_M_RD) {
511                         ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
512                 } else {
513                         ret = gmbus_xfer_write(dev_priv, &msgs[i]);
514                 }
515
516                 if (ret == -ETIMEDOUT)
517                         goto timeout;
518                 if (ret == -ENXIO)
519                         goto clear_err;
520
521                 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE,
522                                            GMBUS_HW_WAIT_EN);
523                 if (ret == -ENXIO)
524                         goto clear_err;
525                 if (ret)
526                         goto timeout;
527         }
528
529         /* Generate a STOP condition on the bus. Note that gmbus can't generata
530          * a STOP on the very first cycle. To simplify the code we
531          * unconditionally generate the STOP condition with an additional gmbus
532          * cycle. */
533         I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
534
535         /* Mark the GMBUS interface as disabled after waiting for idle.
536          * We will re-enable it at the start of the next xfer,
537          * till then let it sleep.
538          */
539         if (gmbus_wait_idle(dev_priv)) {
540                 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
541                          sc->name);
542                 ret = -ETIMEDOUT;
543         }
544         I915_WRITE(GMBUS0 + reg_offset, 0);
545         ret = ret ?: i;
546         goto timeout;   /* XXX: should be out */
547
548 clear_err:
549         /*
550          * Wait for bus to IDLE before clearing NAK.
551          * If we clear the NAK while bus is still active, then it will stay
552          * active and the next transaction may fail.
553          *
554          * If no ACK is received during the address phase of a transaction, the
555          * adapter must report -ENXIO. It is not clear what to return if no ACK
556          * is received at other times. But we have to be careful to not return
557          * spurious -ENXIO because that will prevent i2c and drm edid functions
558          * from retrying. So return -ENXIO only when gmbus properly quiescents -
559          * timing out seems to happen when there _is_ a ddc chip present, but
560          * it's slow responding and only answers on the 2nd retry.
561          */
562         ret = -ENXIO;
563         if (gmbus_wait_idle(dev_priv)) {
564                 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
565                               sc->name);
566                 ret = -ETIMEDOUT;
567         }
568
569         /* Toggle the Software Clear Interrupt bit. This has the effect
570          * of resetting the GMBUS controller and so clearing the
571          * BUS_ERROR raised by the slave's NAK.
572          */
573         I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
574         I915_WRITE(GMBUS1 + reg_offset, 0);
575         I915_WRITE(GMBUS0 + reg_offset, 0);
576
577         DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
578                          sc->name, msgs[i].slave,
579                          (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
580
581         goto out;
582
583 timeout:
584         DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
585                  sc->name, sc->reg0 & 0xff);
586         I915_WRITE(GMBUS0 + reg_offset, 0);
587
588         /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
589         sc->force_bit_dev = true;
590         ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num);
591
592 out:
593         mutex_unlock(&dev_priv->gmbus_mutex);
594         return ret;
595 }
596
597 struct device *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
598                                             unsigned port)
599 {
600         WARN_ON(!intel_gmbus_is_port_valid(port));
601         /* -1 to map pin pair to gmbus index */
602         return (intel_gmbus_is_port_valid(port)) ?
603                 dev_priv->gmbus[port-1] : NULL;
604 }
605
606 void
607 intel_gmbus_set_speed(device_t idev, int speed)
608 {
609         struct intel_iic_softc *sc;
610
611         sc = device_get_softc(device_get_parent(idev));
612
613         sc->reg0 = (sc->reg0 & ~(0x3 << 8)) | speed;
614 }
615
616 void
617 intel_gmbus_force_bit(device_t idev, bool force_bit)
618 {
619         struct intel_iic_softc *sc;
620
621         sc = device_get_softc(device_get_parent(idev));
622         sc->force_bit_dev += force_bit ? 1 : -1;
623         DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
624                       force_bit ? "en" : "dis", sc->name,
625                       sc->force_bit_dev);
626 }
627
628 static int
629 intel_gmbus_probe(device_t dev)
630 {
631
632         return (BUS_PROBE_SPECIFIC);
633 }
634
635 static int
636 intel_gmbus_attach(device_t idev)
637 {
638         struct drm_i915_private *dev_priv;
639         struct intel_iic_softc *sc;
640         int pin;
641
642         sc = device_get_softc(idev);
643         sc->drm_dev = device_get_softc(device_get_parent(idev));
644         dev_priv = sc->drm_dev->dev_private;
645         pin = device_get_unit(idev);
646
647         ksnprintf(sc->name, sizeof(sc->name), "gmbus bus %s", gpio_names[pin]);
648         device_set_desc(idev, sc->name);
649
650         /* By default use a conservative clock rate */
651         sc->reg0 = (pin + 1) | GMBUS_RATE_100KHZ;
652
653         /* XXX force bit banging until GMBUS is fully debugged */
654         if (IS_GEN2(sc->drm_dev)) {
655                 sc->force_bit_dev = true;
656         }
657
658         /* add bus interface device */
659         sc->iic_dev = device_add_child(idev, "iicbus", -1);
660         if (sc->iic_dev == NULL)
661                 return (ENXIO);
662         device_quiet(sc->iic_dev);
663         bus_generic_attach(idev);
664
665         return (0);
666 }
667
668 static int
669 intel_gmbus_detach(device_t idev)
670 {
671         struct intel_iic_softc *sc;
672         struct drm_i915_private *dev_priv;
673         device_t child;
674         int u;
675
676         sc = device_get_softc(idev);
677         u = device_get_unit(idev);
678         dev_priv = sc->drm_dev->dev_private;
679
680         child = sc->iic_dev;
681         bus_generic_detach(idev);
682         if (child != NULL)
683                 device_delete_child(idev, child);
684
685         return (0);
686 }
687
688 static int
689 intel_iicbb_probe(device_t dev)
690 {
691
692         return (BUS_PROBE_DEFAULT);
693 }
694
695 static int
696 intel_iicbb_detach(device_t idev)
697 {
698         struct intel_iic_softc *sc;
699         device_t child;
700
701         sc = device_get_softc(idev);
702         child = sc->iic_dev;
703         bus_generic_detach(idev);
704         if (child)
705                 device_delete_child(idev, child);
706         return (0);
707 }
708
709 static device_method_t intel_gmbus_methods[] = {
710         DEVMETHOD(device_probe,         intel_gmbus_probe),
711         DEVMETHOD(device_attach,        intel_gmbus_attach),
712         DEVMETHOD(device_detach,        intel_gmbus_detach),
713         DEVMETHOD(iicbus_reset,         intel_iicbus_reset),
714         DEVMETHOD(iicbus_transfer,      gmbus_xfer),
715         DEVMETHOD_END
716 };
717 static driver_t intel_gmbus_driver = {
718         "intel_gmbus",
719         intel_gmbus_methods,
720         sizeof(struct intel_iic_softc)
721 };
722 static devclass_t intel_gmbus_devclass;
723 DRIVER_MODULE_ORDERED(intel_gmbus, drm, intel_gmbus_driver,
724     intel_gmbus_devclass, 0, 0, SI_ORDER_FIRST);
725 DRIVER_MODULE(iicbus, intel_gmbus, iicbus_driver, iicbus_devclass, NULL, NULL);
726
727 static device_method_t intel_iicbb_methods[] =  {
728         DEVMETHOD(device_probe,         intel_iicbb_probe),
729         DEVMETHOD(device_attach,        intel_gpio_setup),
730         DEVMETHOD(device_detach,        intel_iicbb_detach),
731
732         DEVMETHOD(bus_add_child,        bus_generic_add_child),
733         DEVMETHOD(bus_print_child,      bus_generic_print_child),
734
735         DEVMETHOD(iicbb_callback,       iicbus_null_callback),
736         DEVMETHOD(iicbb_reset,          intel_iicbus_reset),
737         DEVMETHOD(iicbb_setsda,         set_data),
738         DEVMETHOD(iicbb_setscl,         set_clock),
739         DEVMETHOD(iicbb_getsda,         get_data),
740         DEVMETHOD(iicbb_getscl,         get_clock),
741         DEVMETHOD_END
742 };
743 static driver_t intel_iicbb_driver = {
744         "intel_iicbb",
745         intel_iicbb_methods,
746         sizeof(struct intel_iic_softc)
747 };
748 static devclass_t intel_iicbb_devclass;
749 DRIVER_MODULE_ORDERED(intel_iicbb, drm, intel_iicbb_driver,
750     intel_iicbb_devclass, 0, 0, SI_ORDER_FIRST);
751 DRIVER_MODULE(iicbb, intel_iicbb, iicbb_driver, iicbb_devclass, NULL, NULL);
752
753 static void intel_teardown_gmbus_m(struct drm_device *dev, int m);
754
755 int
756 intel_setup_gmbus(struct drm_device *dev)
757 {
758         struct drm_i915_private *dev_priv = dev->dev_private;
759         device_t iic_dev;
760         int i, ret;
761
762         if (HAS_PCH_SPLIT(dev))
763                 dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
764         else if (IS_VALLEYVIEW(dev))
765                 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
766         else
767                 dev_priv->gpio_mmio_base = 0;
768
769         lockinit(&dev_priv->gmbus_mutex, "gmbus", 0, LK_CANRECURSE);
770         init_waitqueue_head(&dev_priv->gmbus_wait_queue);
771
772         dev_priv->gmbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
773             M_DRM, M_WAITOK | M_ZERO);
774         dev_priv->bbbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
775             M_DRM, M_WAITOK | M_ZERO);
776         dev_priv->gmbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
777             M_DRM, M_WAITOK | M_ZERO);
778         dev_priv->bbbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PORTS,
779             M_DRM, M_WAITOK | M_ZERO);
780
781         for (i = 0; i < GMBUS_NUM_PORTS; i++) {
782                 /*
783                  * Initialized bbbus_bridge before gmbus_bridge, since
784                  * gmbus may decide to force quirk transfer in the
785                  * attachment code.
786                  */
787                 dev_priv->bbbus_bridge[i] = device_add_child(dev->dev,
788                     "intel_iicbb", i);
789                 if (dev_priv->bbbus_bridge[i] == NULL) {
790                         DRM_ERROR("bbbus bridge %d creation failed\n", i);
791                         ret = ENXIO;
792                         goto err;
793                 }
794                 device_quiet(dev_priv->bbbus_bridge[i]);
795                 ret = device_probe_and_attach(dev_priv->bbbus_bridge[i]);
796                 if (ret != 0) {
797                         DRM_ERROR("bbbus bridge %d attach failed, %d\n", i,
798                             ret);
799                         goto err;
800                 }
801
802                 iic_dev = device_find_child(dev_priv->bbbus_bridge[i], "iicbb",
803                     -1);
804                 if (iic_dev == NULL) {
805                         DRM_ERROR("bbbus bridge doesn't have iicbb child\n");
806                         goto err;
807                 }
808                 iic_dev = device_find_child(iic_dev, "iicbus", -1);
809                 if (iic_dev == NULL) {
810                         DRM_ERROR(
811                 "bbbus bridge doesn't have iicbus grandchild\n");
812                         goto err;
813                 }
814
815                 dev_priv->bbbus[i] = iic_dev;
816
817                 dev_priv->gmbus_bridge[i] = device_add_child(dev->dev,
818                     "intel_gmbus", i);
819                 if (dev_priv->gmbus_bridge[i] == NULL) {
820                         DRM_ERROR("gmbus bridge %d creation failed\n", i);
821                         ret = ENXIO;
822                         goto err;
823                 }
824                 device_quiet(dev_priv->gmbus_bridge[i]);
825                 ret = device_probe_and_attach(dev_priv->gmbus_bridge[i]);
826                 if (ret != 0) {
827                         DRM_ERROR("gmbus bridge %d attach failed, %d\n", i,
828                             ret);
829                         ret = ENXIO;
830                         goto err;
831                 }
832
833                 iic_dev = device_find_child(dev_priv->gmbus_bridge[i],
834                     "iicbus", -1);
835                 if (iic_dev == NULL) {
836                         DRM_ERROR("gmbus bridge doesn't have iicbus child\n");
837                         goto err;
838                 }
839                 dev_priv->gmbus[i] = iic_dev;
840
841                 intel_i2c_reset(dev);
842         }
843
844         return (0);
845
846 err:
847         intel_teardown_gmbus_m(dev, i);
848         return (ret);
849 }
850
851 static void
852 intel_teardown_gmbus_m(struct drm_device *dev, int m)
853 {
854         struct drm_i915_private *dev_priv;
855
856         dev_priv = dev->dev_private;
857
858         drm_free(dev_priv->gmbus, M_DRM);
859         dev_priv->gmbus = NULL;
860         drm_free(dev_priv->bbbus, M_DRM);
861         dev_priv->bbbus = NULL;
862         drm_free(dev_priv->gmbus_bridge, M_DRM);
863         dev_priv->gmbus_bridge = NULL;
864         drm_free(dev_priv->bbbus_bridge, M_DRM);
865         dev_priv->bbbus_bridge = NULL;
866         lockuninit(&dev_priv->gmbus_mutex);
867 }
868
869 void
870 intel_teardown_gmbus(struct drm_device *dev)
871 {
872
873         get_mplock();
874         intel_teardown_gmbus_m(dev, GMBUS_NUM_PORTS);
875         rel_mplock();
876 }