Home | History | Annotate | Line # | Download | only in pci
      1  1.1  gdamore /*-
      2  1.1  gdamore  * Copyright (c) 2006 Itronix Inc.
      3  1.1  gdamore  * All rights reserved.
      4  1.1  gdamore  *
      5  1.1  gdamore  * Written by Garrett D'Amore for Itronix Inc.
      6  1.1  gdamore  *
      7  1.1  gdamore  * Redistribution and use in source and binary forms, with or without
      8  1.1  gdamore  * modification, are permitted provided that the following conditions
      9  1.1  gdamore  * are met:
     10  1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     11  1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     12  1.1  gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  gdamore  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  gdamore  *    documentation and/or other materials provided with the distribution.
     15  1.1  gdamore  * 3. The name of Itronix Inc. may not be used to endorse
     16  1.1  gdamore  *    or promote products derived from this software without specific
     17  1.1  gdamore  *    prior written permission.
     18  1.1  gdamore  *
     19  1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
     20  1.1  gdamore  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  1.1  gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     23  1.1  gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  gdamore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     25  1.1  gdamore  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  gdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     27  1.1  gdamore  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     28  1.1  gdamore  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     29  1.1  gdamore  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  1.1  gdamore  */
     31  1.1  gdamore 
     32  1.1  gdamore /*
     33  1.1  gdamore  * ATI Technologies Inc. ("ATI") has not assisted in the creation of, and
     34  1.1  gdamore  * does not endorse, this software.  ATI will not be responsible or liable
     35  1.1  gdamore  * for any actual or alleged damage or loss caused by or in connection with
     36  1.1  gdamore  * the use of or reliance on this software.
     37  1.1  gdamore  */
     38  1.1  gdamore 
     39  1.1  gdamore #include <sys/cdefs.h>
     40  1.4  thorpej __KERNEL_RCSID(0, "$NetBSD: radeonfb_i2c.c,v 1.4 2022/09/25 17:52:25 thorpej Exp $");
     41  1.1  gdamore 
     42  1.1  gdamore #include <sys/param.h>
     43  1.1  gdamore #include <sys/systm.h>
     44  1.1  gdamore #include <sys/device.h>
     45  1.1  gdamore #include <sys/systm.h>
     46  1.2       ad #include <sys/bus.h>
     47  1.1  gdamore 
     48  1.1  gdamore #include <dev/i2c/i2cvar.h>
     49  1.1  gdamore #include <dev/i2c/i2c_bitbang.h>
     50  1.1  gdamore #include <dev/i2c/ddcvar.h>
     51  1.1  gdamore 
     52  1.1  gdamore #include <dev/pci/radeonfbreg.h>
     53  1.1  gdamore #include <dev/pci/radeonfbvar.h>
     54  1.1  gdamore 
     55  1.1  gdamore /* i2c support */
     56  1.1  gdamore static int radeonfb_i2c_acquire_bus(void *, int);
     57  1.1  gdamore static void radeonfb_i2c_release_bus(void *, int);
     58  1.1  gdamore static int radeonfb_i2c_send_start(void *, int);
     59  1.1  gdamore static int radeonfb_i2c_send_stop(void *, int);
     60  1.1  gdamore static int radeonfb_i2c_initiate_xfer(void *, i2c_addr_t, int);
     61  1.1  gdamore static int radeonfb_i2c_read_byte(void *, uint8_t *, int);
     62  1.1  gdamore static int radeonfb_i2c_write_byte(void *, uint8_t, int);
     63  1.1  gdamore 
     64  1.1  gdamore /* i2c bit-bang glue */
     65  1.1  gdamore static void radeonfb_i2cbb_set_bits(void *, uint32_t);
     66  1.1  gdamore static void radeonfb_i2cbb_set_dir(void *, uint32_t);
     67  1.1  gdamore static uint32_t radeonfb_i2cbb_read(void *);
     68  1.1  gdamore 
     69  1.1  gdamore /*
     70  1.1  gdamore  * I2C bit-bang operations
     71  1.1  gdamore  */
     72  1.1  gdamore void
     73  1.1  gdamore radeonfb_i2cbb_set_bits(void *cookie, uint32_t bits)
     74  1.1  gdamore {
     75  1.1  gdamore 	struct radeonfb_i2c	*ric = (struct radeonfb_i2c *)cookie;
     76  1.1  gdamore 	struct radeonfb_softc	*sc = ric->ric_softc;
     77  1.1  gdamore 
     78  1.1  gdamore 	PATCH32(sc, ric->ric_register, bits,
     79  1.1  gdamore 	    ~(RADEON_GPIO_A_0 | RADEON_GPIO_A_1));
     80  1.1  gdamore }
     81  1.1  gdamore 
     82  1.1  gdamore void
     83  1.1  gdamore radeonfb_i2cbb_set_dir(void *cookie, uint32_t bits)
     84  1.1  gdamore {
     85  1.1  gdamore 	struct radeonfb_i2c	*ric = (struct radeonfb_i2c *)cookie;
     86  1.1  gdamore 	struct radeonfb_softc	*sc = ric->ric_softc;
     87  1.1  gdamore 
     88  1.1  gdamore 	PATCH32(sc, ric->ric_register, bits, ~(RADEON_GPIO_EN_0));
     89  1.1  gdamore }
     90  1.1  gdamore 
     91  1.1  gdamore uint32_t
     92  1.1  gdamore radeonfb_i2cbb_read(void *cookie)
     93  1.1  gdamore {
     94  1.1  gdamore 	struct radeonfb_i2c	*ric = (struct radeonfb_i2c *)cookie;
     95  1.1  gdamore 	struct radeonfb_softc	*sc = ric->ric_softc;
     96  1.1  gdamore 
     97  1.1  gdamore 	/* output bit is 0 shifted, input bit is shifted 8 */
     98  1.1  gdamore 	return (GET32(sc, ric->ric_register) >> RADEON_GPIO_Y_SHIFT_0);
     99  1.1  gdamore }
    100  1.1  gdamore 
    101  1.1  gdamore static const struct i2c_bitbang_ops radeonfb_i2cbb_ops = {
    102  1.1  gdamore 	radeonfb_i2cbb_set_bits,
    103  1.1  gdamore 	radeonfb_i2cbb_set_dir,
    104  1.1  gdamore 	radeonfb_i2cbb_read,
    105  1.1  gdamore 	{
    106  1.1  gdamore 		RADEON_GPIO_A_0,	/* SDA */
    107  1.1  gdamore 		RADEON_GPIO_A_1,	/* SCL */
    108  1.1  gdamore 		RADEON_GPIO_EN_0,	/* SDA output */
    109  1.1  gdamore 		0,			/* SDA input */
    110  1.1  gdamore 	}
    111  1.1  gdamore };
    112  1.1  gdamore 
    113  1.1  gdamore /*
    114  1.1  gdamore  * I2C support
    115  1.1  gdamore  */
    116  1.1  gdamore int
    117  1.1  gdamore radeonfb_i2c_acquire_bus(void *cookie, int flags)
    118  1.1  gdamore {
    119  1.1  gdamore 	struct radeonfb_i2c	*ric = (struct radeonfb_i2c *)cookie;
    120  1.1  gdamore 	struct radeonfb_softc	*sc = ric->ric_softc;
    121  1.1  gdamore 	int			i;
    122  1.1  gdamore 
    123  1.1  gdamore 	/*
    124  1.1  gdamore 	 * Some hardware seems to have hardware/software combined access
    125  1.1  gdamore 	 * to the DVI I2C.  We want to use software.
    126  1.1  gdamore 	 */
    127  1.1  gdamore 	if (ric->ric_register == RADEON_GPIO_DVI_DDC) {
    128  1.1  gdamore 
    129  1.1  gdamore 		/* ask for software access to I2C bus */
    130  1.1  gdamore 		SET32(sc, ric->ric_register, RADEON_GPIO_SW_USE);
    131  1.1  gdamore 
    132  1.1  gdamore 		/*
    133  1.1  gdamore 		 * wait for the chip to give up access.  we don't make
    134  1.1  gdamore 		 * this a hard timeout, because some hardware might
    135  1.1  gdamore 		 * not implement this negotiation protocol
    136  1.1  gdamore 		 */
    137  1.1  gdamore 		for (i = RADEON_TIMEOUT; i; i--) {
    138  1.1  gdamore 			if (GET32(sc, ric->ric_register) & RADEON_GPIO_SW_USE)
    139  1.1  gdamore 				break;
    140  1.1  gdamore 		}
    141  1.1  gdamore 	}
    142  1.1  gdamore 
    143  1.1  gdamore 	/* enable the I2C clock */
    144  1.1  gdamore 	SET32(sc, ric->ric_register, RADEON_GPIO_EN_1);
    145  1.1  gdamore 
    146  1.1  gdamore 	return 0;
    147  1.1  gdamore }
    148  1.1  gdamore 
    149  1.1  gdamore void
    150  1.1  gdamore radeonfb_i2c_release_bus(void *cookie, int flags)
    151  1.1  gdamore {
    152  1.1  gdamore 	struct radeonfb_i2c	*ric = (struct radeonfb_i2c *)cookie;
    153  1.1  gdamore 	struct radeonfb_softc	*sc = ric->ric_softc;
    154  1.1  gdamore 
    155  1.1  gdamore 	if (ric->ric_register == RADEON_GPIO_DVI_DDC) {
    156  1.1  gdamore 		/* we no longer "want" I2C, and we're "done" with it */
    157  1.1  gdamore 		CLR32(sc, ric->ric_register, RADEON_GPIO_SW_USE);
    158  1.1  gdamore 		SET32(sc, ric->ric_register, RADEON_GPIO_SW_DONE);
    159  1.1  gdamore 	}
    160  1.1  gdamore }
    161  1.1  gdamore 
    162  1.1  gdamore int
    163  1.1  gdamore radeonfb_i2c_send_start(void *cookie, int flags)
    164  1.1  gdamore {
    165  1.1  gdamore 
    166  1.1  gdamore 	return i2c_bitbang_send_start(cookie, flags, &radeonfb_i2cbb_ops);
    167  1.1  gdamore }
    168  1.1  gdamore 
    169  1.1  gdamore int
    170  1.1  gdamore radeonfb_i2c_send_stop(void *cookie, int flags)
    171  1.1  gdamore {
    172  1.1  gdamore 
    173  1.1  gdamore 	return i2c_bitbang_send_stop(cookie, flags, &radeonfb_i2cbb_ops);
    174  1.1  gdamore }
    175  1.1  gdamore 
    176  1.1  gdamore int
    177  1.1  gdamore radeonfb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    178  1.1  gdamore {
    179  1.1  gdamore 
    180  1.1  gdamore 	return i2c_bitbang_initiate_xfer(cookie, addr, flags,
    181  1.1  gdamore 	    &radeonfb_i2cbb_ops);
    182  1.1  gdamore }
    183  1.1  gdamore 
    184  1.1  gdamore int
    185  1.1  gdamore radeonfb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
    186  1.1  gdamore {
    187  1.1  gdamore 
    188  1.1  gdamore 	return i2c_bitbang_read_byte(cookie, valp, flags, &radeonfb_i2cbb_ops);
    189  1.1  gdamore }
    190  1.1  gdamore 
    191  1.1  gdamore int
    192  1.1  gdamore radeonfb_i2c_write_byte(void *cookie, uint8_t val, int flags)
    193  1.1  gdamore {
    194  1.1  gdamore 
    195  1.1  gdamore 	return i2c_bitbang_write_byte(cookie, val, flags, &radeonfb_i2cbb_ops);
    196  1.1  gdamore }
    197  1.1  gdamore 
    198  1.1  gdamore void
    199  1.1  gdamore radeonfb_i2c_init(struct radeonfb_softc *sc)
    200  1.1  gdamore {
    201  1.1  gdamore 	int	i;
    202  1.1  gdamore 
    203  1.1  gdamore 	for (i = 0; i < 4; i++) {
    204  1.1  gdamore 		struct i2c_controller	*icc = &sc->sc_i2c[i].ric_controller;
    205  1.1  gdamore 
    206  1.1  gdamore 		sc->sc_i2c[i].ric_softc = sc;
    207  1.3  thorpej 		iic_tag_init(icc);
    208  1.1  gdamore 		icc->ic_cookie = &sc->sc_i2c[i];
    209  1.1  gdamore 		icc->ic_acquire_bus = radeonfb_i2c_acquire_bus;
    210  1.1  gdamore 		icc->ic_release_bus = radeonfb_i2c_release_bus;
    211  1.1  gdamore 		icc->ic_send_start = radeonfb_i2c_send_start;
    212  1.1  gdamore 		icc->ic_send_stop = radeonfb_i2c_send_stop;
    213  1.1  gdamore 		icc->ic_initiate_xfer = radeonfb_i2c_initiate_xfer;
    214  1.1  gdamore 		icc->ic_read_byte = radeonfb_i2c_read_byte;
    215  1.1  gdamore 		icc->ic_write_byte = radeonfb_i2c_write_byte;
    216  1.1  gdamore 	}
    217  1.1  gdamore 
    218  1.1  gdamore 	/* index == ddctype (RADEON_DDC_XX) - 1 */
    219  1.1  gdamore 	sc->sc_i2c[0].ric_register = RADEON_GPIO_MONID;
    220  1.1  gdamore 	sc->sc_i2c[1].ric_register = RADEON_GPIO_DVI_DDC;
    221  1.1  gdamore 	sc->sc_i2c[2].ric_register = RADEON_GPIO_VGA_DDC;
    222  1.1  gdamore 	sc->sc_i2c[3].ric_register = RADEON_GPIO_CRT2_DDC;
    223  1.1  gdamore }
    224  1.1  gdamore 
    225  1.1  gdamore int
    226  1.1  gdamore radeonfb_i2c_read_edid(struct radeonfb_softc *sc, int ddctype, uint8_t *data)
    227  1.1  gdamore {
    228  1.1  gdamore 
    229  1.1  gdamore 	if ((ddctype < 1) || (ddctype > 4))
    230  1.1  gdamore 		return EINVAL;
    231  1.1  gdamore 
    232  1.1  gdamore 	ddctype--;
    233  1.1  gdamore 	return (ddc_read_edid(&sc->sc_i2c[ddctype].ric_controller, data, 128));
    234  1.1  gdamore }
    235