Home | History | Annotate | Line # | Download | only in pci
radeonfb_i2c.c revision 1.2.108.1
      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.2.108.1   martin __KERNEL_RCSID(0, "$NetBSD: radeonfb_i2c.c,v 1.2.108.1 2020/04/08 14:08:10 martin 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/malloc.h>
     46        1.1  gdamore #include <sys/systm.h>
     47        1.2       ad #include <sys/bus.h>
     48        1.1  gdamore 
     49        1.1  gdamore #include <dev/i2c/i2cvar.h>
     50        1.1  gdamore #include <dev/i2c/i2c_bitbang.h>
     51        1.1  gdamore #include <dev/i2c/ddcvar.h>
     52        1.1  gdamore 
     53        1.1  gdamore #include <dev/pci/radeonfbreg.h>
     54        1.1  gdamore #include <dev/pci/radeonfbvar.h>
     55        1.1  gdamore 
     56        1.1  gdamore /* i2c support */
     57        1.1  gdamore static int radeonfb_i2c_acquire_bus(void *, int);
     58        1.1  gdamore static void radeonfb_i2c_release_bus(void *, int);
     59        1.1  gdamore static int radeonfb_i2c_send_start(void *, int);
     60        1.1  gdamore static int radeonfb_i2c_send_stop(void *, int);
     61        1.1  gdamore static int radeonfb_i2c_initiate_xfer(void *, i2c_addr_t, int);
     62        1.1  gdamore static int radeonfb_i2c_read_byte(void *, uint8_t *, int);
     63        1.1  gdamore static int radeonfb_i2c_write_byte(void *, uint8_t, int);
     64        1.1  gdamore 
     65        1.1  gdamore /* i2c bit-bang glue */
     66        1.1  gdamore static void radeonfb_i2cbb_set_bits(void *, uint32_t);
     67        1.1  gdamore static void radeonfb_i2cbb_set_dir(void *, uint32_t);
     68        1.1  gdamore static uint32_t radeonfb_i2cbb_read(void *);
     69        1.1  gdamore 
     70        1.1  gdamore /*
     71        1.1  gdamore  * I2C bit-bang operations
     72        1.1  gdamore  */
     73        1.1  gdamore void
     74        1.1  gdamore radeonfb_i2cbb_set_bits(void *cookie, uint32_t bits)
     75        1.1  gdamore {
     76        1.1  gdamore 	struct radeonfb_i2c	*ric = (struct radeonfb_i2c *)cookie;
     77        1.1  gdamore 	struct radeonfb_softc	*sc = ric->ric_softc;
     78        1.1  gdamore 
     79        1.1  gdamore 	PATCH32(sc, ric->ric_register, bits,
     80        1.1  gdamore 	    ~(RADEON_GPIO_A_0 | RADEON_GPIO_A_1));
     81        1.1  gdamore }
     82        1.1  gdamore 
     83        1.1  gdamore void
     84        1.1  gdamore radeonfb_i2cbb_set_dir(void *cookie, uint32_t bits)
     85        1.1  gdamore {
     86        1.1  gdamore 	struct radeonfb_i2c	*ric = (struct radeonfb_i2c *)cookie;
     87        1.1  gdamore 	struct radeonfb_softc	*sc = ric->ric_softc;
     88        1.1  gdamore 
     89        1.1  gdamore 	PATCH32(sc, ric->ric_register, bits, ~(RADEON_GPIO_EN_0));
     90        1.1  gdamore }
     91        1.1  gdamore 
     92        1.1  gdamore uint32_t
     93        1.1  gdamore radeonfb_i2cbb_read(void *cookie)
     94        1.1  gdamore {
     95        1.1  gdamore 	struct radeonfb_i2c	*ric = (struct radeonfb_i2c *)cookie;
     96        1.1  gdamore 	struct radeonfb_softc	*sc = ric->ric_softc;
     97        1.1  gdamore 
     98        1.1  gdamore 	/* output bit is 0 shifted, input bit is shifted 8 */
     99        1.1  gdamore 	return (GET32(sc, ric->ric_register) >> RADEON_GPIO_Y_SHIFT_0);
    100        1.1  gdamore }
    101        1.1  gdamore 
    102        1.1  gdamore static const struct i2c_bitbang_ops radeonfb_i2cbb_ops = {
    103        1.1  gdamore 	radeonfb_i2cbb_set_bits,
    104        1.1  gdamore 	radeonfb_i2cbb_set_dir,
    105        1.1  gdamore 	radeonfb_i2cbb_read,
    106        1.1  gdamore 	{
    107        1.1  gdamore 		RADEON_GPIO_A_0,	/* SDA */
    108        1.1  gdamore 		RADEON_GPIO_A_1,	/* SCL */
    109        1.1  gdamore 		RADEON_GPIO_EN_0,	/* SDA output */
    110        1.1  gdamore 		0,			/* SDA input */
    111        1.1  gdamore 	}
    112        1.1  gdamore };
    113        1.1  gdamore 
    114        1.1  gdamore /*
    115        1.1  gdamore  * I2C support
    116        1.1  gdamore  */
    117        1.1  gdamore int
    118        1.1  gdamore radeonfb_i2c_acquire_bus(void *cookie, int flags)
    119        1.1  gdamore {
    120        1.1  gdamore 	struct radeonfb_i2c	*ric = (struct radeonfb_i2c *)cookie;
    121        1.1  gdamore 	struct radeonfb_softc	*sc = ric->ric_softc;
    122        1.1  gdamore 	int			i;
    123        1.1  gdamore 
    124        1.1  gdamore 	/*
    125        1.1  gdamore 	 * Some hardware seems to have hardware/software combined access
    126        1.1  gdamore 	 * to the DVI I2C.  We want to use software.
    127        1.1  gdamore 	 */
    128        1.1  gdamore 	if (ric->ric_register == RADEON_GPIO_DVI_DDC) {
    129        1.1  gdamore 
    130        1.1  gdamore 		/* ask for software access to I2C bus */
    131        1.1  gdamore 		SET32(sc, ric->ric_register, RADEON_GPIO_SW_USE);
    132        1.1  gdamore 
    133        1.1  gdamore 		/*
    134        1.1  gdamore 		 * wait for the chip to give up access.  we don't make
    135        1.1  gdamore 		 * this a hard timeout, because some hardware might
    136        1.1  gdamore 		 * not implement this negotiation protocol
    137        1.1  gdamore 		 */
    138        1.1  gdamore 		for (i = RADEON_TIMEOUT; i; i--) {
    139        1.1  gdamore 			if (GET32(sc, ric->ric_register) & RADEON_GPIO_SW_USE)
    140        1.1  gdamore 				break;
    141        1.1  gdamore 		}
    142        1.1  gdamore 	}
    143        1.1  gdamore 
    144        1.1  gdamore 	/* enable the I2C clock */
    145        1.1  gdamore 	SET32(sc, ric->ric_register, RADEON_GPIO_EN_1);
    146        1.1  gdamore 
    147        1.1  gdamore 	return 0;
    148        1.1  gdamore }
    149        1.1  gdamore 
    150        1.1  gdamore void
    151        1.1  gdamore radeonfb_i2c_release_bus(void *cookie, int flags)
    152        1.1  gdamore {
    153        1.1  gdamore 	struct radeonfb_i2c	*ric = (struct radeonfb_i2c *)cookie;
    154        1.1  gdamore 	struct radeonfb_softc	*sc = ric->ric_softc;
    155        1.1  gdamore 
    156        1.1  gdamore 	if (ric->ric_register == RADEON_GPIO_DVI_DDC) {
    157        1.1  gdamore 		/* we no longer "want" I2C, and we're "done" with it */
    158        1.1  gdamore 		CLR32(sc, ric->ric_register, RADEON_GPIO_SW_USE);
    159        1.1  gdamore 		SET32(sc, ric->ric_register, RADEON_GPIO_SW_DONE);
    160        1.1  gdamore 	}
    161        1.1  gdamore }
    162        1.1  gdamore 
    163        1.1  gdamore int
    164        1.1  gdamore radeonfb_i2c_send_start(void *cookie, int flags)
    165        1.1  gdamore {
    166        1.1  gdamore 
    167        1.1  gdamore 	return i2c_bitbang_send_start(cookie, flags, &radeonfb_i2cbb_ops);
    168        1.1  gdamore }
    169        1.1  gdamore 
    170        1.1  gdamore int
    171        1.1  gdamore radeonfb_i2c_send_stop(void *cookie, int flags)
    172        1.1  gdamore {
    173        1.1  gdamore 
    174        1.1  gdamore 	return i2c_bitbang_send_stop(cookie, flags, &radeonfb_i2cbb_ops);
    175        1.1  gdamore }
    176        1.1  gdamore 
    177        1.1  gdamore int
    178        1.1  gdamore radeonfb_i2c_initiate_xfer(void *cookie, i2c_addr_t addr, int flags)
    179        1.1  gdamore {
    180        1.1  gdamore 
    181        1.1  gdamore 	return i2c_bitbang_initiate_xfer(cookie, addr, flags,
    182        1.1  gdamore 	    &radeonfb_i2cbb_ops);
    183        1.1  gdamore }
    184        1.1  gdamore 
    185        1.1  gdamore int
    186        1.1  gdamore radeonfb_i2c_read_byte(void *cookie, uint8_t *valp, int flags)
    187        1.1  gdamore {
    188        1.1  gdamore 
    189        1.1  gdamore 	return i2c_bitbang_read_byte(cookie, valp, flags, &radeonfb_i2cbb_ops);
    190        1.1  gdamore }
    191        1.1  gdamore 
    192        1.1  gdamore int
    193        1.1  gdamore radeonfb_i2c_write_byte(void *cookie, uint8_t val, int flags)
    194        1.1  gdamore {
    195        1.1  gdamore 
    196        1.1  gdamore 	return i2c_bitbang_write_byte(cookie, val, flags, &radeonfb_i2cbb_ops);
    197        1.1  gdamore }
    198        1.1  gdamore 
    199        1.1  gdamore void
    200        1.1  gdamore radeonfb_i2c_init(struct radeonfb_softc *sc)
    201        1.1  gdamore {
    202        1.1  gdamore 	int	i;
    203        1.1  gdamore 
    204        1.1  gdamore 	for (i = 0; i < 4; i++) {
    205        1.1  gdamore 		struct i2c_controller	*icc = &sc->sc_i2c[i].ric_controller;
    206        1.1  gdamore 
    207        1.1  gdamore 		sc->sc_i2c[i].ric_softc = sc;
    208  1.2.108.1   martin 		iic_tag_init(icc);
    209        1.1  gdamore 		icc->ic_cookie = &sc->sc_i2c[i];
    210        1.1  gdamore 		icc->ic_acquire_bus = radeonfb_i2c_acquire_bus;
    211        1.1  gdamore 		icc->ic_release_bus = radeonfb_i2c_release_bus;
    212        1.1  gdamore 		icc->ic_send_start = radeonfb_i2c_send_start;
    213        1.1  gdamore 		icc->ic_send_stop = radeonfb_i2c_send_stop;
    214        1.1  gdamore 		icc->ic_initiate_xfer = radeonfb_i2c_initiate_xfer;
    215        1.1  gdamore 		icc->ic_read_byte = radeonfb_i2c_read_byte;
    216        1.1  gdamore 		icc->ic_write_byte = radeonfb_i2c_write_byte;
    217        1.1  gdamore 	}
    218        1.1  gdamore 
    219        1.1  gdamore 	/* index == ddctype (RADEON_DDC_XX) - 1 */
    220        1.1  gdamore 	sc->sc_i2c[0].ric_register = RADEON_GPIO_MONID;
    221        1.1  gdamore 	sc->sc_i2c[1].ric_register = RADEON_GPIO_DVI_DDC;
    222        1.1  gdamore 	sc->sc_i2c[2].ric_register = RADEON_GPIO_VGA_DDC;
    223        1.1  gdamore 	sc->sc_i2c[3].ric_register = RADEON_GPIO_CRT2_DDC;
    224        1.1  gdamore }
    225        1.1  gdamore 
    226        1.1  gdamore int
    227        1.1  gdamore radeonfb_i2c_read_edid(struct radeonfb_softc *sc, int ddctype, uint8_t *data)
    228        1.1  gdamore {
    229        1.1  gdamore 
    230        1.1  gdamore 	if ((ddctype < 1) || (ddctype > 4))
    231        1.1  gdamore 		return EINVAL;
    232        1.1  gdamore 
    233        1.1  gdamore 	ddctype--;
    234        1.1  gdamore 	return (ddc_read_edid(&sc->sc_i2c[ddctype].ric_controller, data, 128));
    235        1.1  gdamore }
    236