Home | History | Annotate | Line # | Download | only in i2c
ddc.c revision 1.3.44.1
      1  1.3.44.1  jdolecek /* $NetBSD: ddc.c,v 1.3.44.1 2017/12/03 11:37:02 jdolecek Exp $ */
      2       1.1   gdamore 
      3       1.1   gdamore /*-
      4       1.1   gdamore  * Copyright (c) 2006 Itronix Inc.
      5       1.1   gdamore  * All rights reserved.
      6       1.1   gdamore  *
      7       1.1   gdamore  * Written by Garrett D'Amore for Itronix Inc.
      8       1.1   gdamore  *
      9       1.1   gdamore  * Redistribution and use in source and binary forms, with or without
     10       1.1   gdamore  * modification, are permitted provided that the following conditions
     11       1.1   gdamore  * are met:
     12       1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     13       1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     14       1.1   gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     15       1.1   gdamore  *    notice, this list of conditions and the following disclaimer in the
     16       1.1   gdamore  *    documentation and/or other materials provided with the distribution.
     17       1.1   gdamore  * 3. The name of Itronix Inc. may not be used to endorse
     18       1.1   gdamore  *    or promote products derived from this software without specific
     19       1.1   gdamore  *    prior written permission.
     20       1.1   gdamore  *
     21       1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
     22       1.1   gdamore  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23       1.1   gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24       1.1   gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25       1.1   gdamore  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26       1.1   gdamore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27       1.1   gdamore  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28       1.1   gdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29       1.1   gdamore  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30       1.1   gdamore  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     31       1.1   gdamore  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1   gdamore  */
     33       1.1   gdamore 
     34       1.1   gdamore #include <sys/cdefs.h>
     35  1.3.44.1  jdolecek __KERNEL_RCSID(0, "$NetBSD: ddc.c,v 1.3.44.1 2017/12/03 11:37:02 jdolecek Exp $");
     36       1.1   gdamore 
     37       1.1   gdamore #include <sys/param.h>
     38       1.1   gdamore #include <sys/systm.h>
     39       1.1   gdamore #include <sys/device.h>
     40       1.1   gdamore #include <sys/kernel.h>
     41       1.1   gdamore 
     42       1.2        ad #include <sys/bus.h>
     43       1.1   gdamore #include <dev/i2c/i2cvar.h>
     44       1.1   gdamore #include <dev/i2c/ddcreg.h>
     45       1.1   gdamore #include <dev/i2c/ddcvar.h>
     46       1.1   gdamore 
     47       1.1   gdamore /*
     48       1.1   gdamore  * VESA Display Data Channel I2C client, used to access EDID
     49       1.1   gdamore  * information.
     50       1.1   gdamore  *
     51       1.1   gdamore  * Note that this only supports DDC version 2, which uses normal I2C.
     52       1.1   gdamore  * The older DDCv1 standard used sync signaling to provide the I2C
     53       1.1   gdamore  * clock, and is typically not used on reasonably recent monitors.
     54       1.1   gdamore  */
     55       1.1   gdamore 
     56       1.1   gdamore struct ddc_softc {
     57       1.1   gdamore 	i2c_tag_t	sc_tag;
     58       1.1   gdamore 	int		sc_address;
     59       1.1   gdamore };
     60       1.1   gdamore 
     61       1.3   xtraeme static int ddc_match(device_t, cfdata_t, void *);
     62       1.3   xtraeme static void ddc_attach(device_t, device_t, void *);
     63       1.1   gdamore 
     64       1.3   xtraeme CFATTACH_DECL_NEW(ddc, sizeof (struct ddc_softc),
     65       1.1   gdamore     ddc_match, ddc_attach, NULL, NULL);
     66       1.1   gdamore 
     67       1.1   gdamore static int
     68       1.3   xtraeme ddc_match(device_t parent, cfdata_t cf, void *aux)
     69       1.1   gdamore {
     70       1.1   gdamore 	struct i2c_attach_args *ia = aux;
     71       1.1   gdamore 
     72       1.1   gdamore 	if (ia->ia_addr == DDC_ADDR)
     73       1.1   gdamore 		return 1;
     74       1.1   gdamore 	return 0;
     75       1.1   gdamore }
     76       1.1   gdamore 
     77       1.1   gdamore static void
     78       1.3   xtraeme ddc_attach(device_t parent, device_t self, void *aux)
     79       1.1   gdamore {
     80       1.1   gdamore 	struct ddc_softc *sc = device_private(self);
     81       1.1   gdamore 	struct i2c_attach_args *ia = aux;
     82       1.1   gdamore 
     83  1.3.44.1  jdolecek 	sc->sc_tag = ia->ia_tag;
     84       1.1   gdamore 	sc->sc_address = ia->ia_addr;
     85       1.1   gdamore 
     86       1.1   gdamore 	aprint_naive(": DDC\n");
     87       1.1   gdamore 	aprint_normal(": DDC\n");
     88       1.1   gdamore }
     89       1.1   gdamore 
     90       1.1   gdamore /* XXX: add cdev ops? */
     91       1.1   gdamore 
     92       1.1   gdamore int
     93       1.1   gdamore ddc_read_edid(i2c_tag_t tag, uint8_t *dest, size_t len)
     94       1.1   gdamore {
     95  1.3.44.1  jdolecek 	return ddc_read_edid_block(tag, dest, len, DDC_EDID_START);
     96  1.3.44.1  jdolecek }
     97  1.3.44.1  jdolecek 
     98  1.3.44.1  jdolecek int
     99  1.3.44.1  jdolecek ddc_read_edid_block(i2c_tag_t tag, uint8_t *dest, size_t len, uint8_t block)
    100  1.3.44.1  jdolecek {
    101  1.3.44.1  jdolecek 	uint8_t edid[256];
    102  1.3.44.1  jdolecek 	uint8_t wbuf[2];
    103  1.3.44.1  jdolecek 	int error;
    104       1.1   gdamore 
    105  1.3.44.1  jdolecek 	if ((error = iic_acquire_bus(tag, I2C_F_POLL)) != 0)
    106  1.3.44.1  jdolecek 		return error;
    107       1.1   gdamore 
    108  1.3.44.1  jdolecek 	wbuf[0] = block >> 1;	/* start address */
    109       1.1   gdamore 
    110  1.3.44.1  jdolecek 	if ((error = iic_exec(tag, I2C_OP_READ_WITH_STOP, DDC_ADDR, wbuf, 1,
    111  1.3.44.1  jdolecek 		edid, sizeof(edid), I2C_F_POLL)) != 0) {
    112       1.1   gdamore 		iic_release_bus(tag, I2C_F_POLL);
    113  1.3.44.1  jdolecek 		return error;
    114       1.1   gdamore 	}
    115       1.1   gdamore 	iic_release_bus(tag, I2C_F_POLL);
    116  1.3.44.1  jdolecek 
    117  1.3.44.1  jdolecek 	if (block & 1) {
    118  1.3.44.1  jdolecek 		memcpy(dest, &edid[128], min(len, 128));
    119  1.3.44.1  jdolecek 	} else {
    120  1.3.44.1  jdolecek 		memcpy(dest, &edid[0], min(len, 128));
    121  1.3.44.1  jdolecek 	}
    122  1.3.44.1  jdolecek 
    123       1.1   gdamore 	return 0;
    124       1.1   gdamore }
    125  1.3.44.1  jdolecek 
    126  1.3.44.1  jdolecek int
    127  1.3.44.1  jdolecek ddc_dev_read_edid(device_t dev, uint8_t *dest, size_t len)
    128  1.3.44.1  jdolecek {
    129  1.3.44.1  jdolecek 	return ddc_dev_read_edid_block(dev, dest, len, DDC_EDID_START);
    130  1.3.44.1  jdolecek }
    131  1.3.44.1  jdolecek 
    132  1.3.44.1  jdolecek int
    133  1.3.44.1  jdolecek ddc_dev_read_edid_block(device_t dev, uint8_t *dest, size_t len, uint8_t block)
    134  1.3.44.1  jdolecek {
    135  1.3.44.1  jdolecek 	if (!device_is_a(dev, "ddc"))
    136  1.3.44.1  jdolecek 		return EINVAL;
    137  1.3.44.1  jdolecek 
    138  1.3.44.1  jdolecek 	const struct ddc_softc *sc = device_private(dev);
    139  1.3.44.1  jdolecek 	return ddc_read_edid_block(sc->sc_tag, dest, len, block);
    140  1.3.44.1  jdolecek }
    141