i810-i2c.c 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
 /*-*- linux-c -*-
 *  linux/drivers/video/i810-i2c.c -- Intel 810/815 I2C support
 *
 *      Copyright (C) 2004 Antonino Daplas<adaplas@pol.net>
 *      All Rights Reserved
 *
 *  This file is subject to the terms and conditions of the GNU General Public
 *  License. See the file COPYING in the main directory of this archive for
 *  more details.
 */
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
14
#include <linux/gfp.h>
15 16 17 18
#include <linux/pci.h>
#include <linux/fb.h>
#include "i810.h"
#include "i810_regs.h"
19
#include "i810_main.h"
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "../edid.h"

/* bit locations in the registers */
#define SCL_DIR_MASK		0x0001
#define SCL_DIR			0x0002
#define SCL_VAL_MASK		0x0004
#define SCL_VAL_OUT		0x0008
#define SCL_VAL_IN		0x0010
#define SDA_DIR_MASK		0x0100
#define SDA_DIR			0x0200
#define SDA_VAL_MASK		0x0400
#define SDA_VAL_OUT		0x0800
#define SDA_VAL_IN		0x1000

#define DEBUG  /* define this for verbose EDID parsing output */

#ifdef DEBUG
#define DPRINTK(fmt, args...) printk(fmt,## args)
#else
#define DPRINTK(fmt, args...)
#endif

static void i810i2c_setscl(void *data, int state)
{
44
        struct i810fb_i2c_chan    *chan = data;
45
        struct i810fb_par         *par = chan->par;
A
Al Viro 已提交
46
	u8                        __iomem *mmio = par->mmio_start_virtual;
47

48 49 50 51
	if (state)
		i810_writel(mmio, chan->ddc_base, SCL_DIR_MASK | SCL_VAL_MASK);
	else
		i810_writel(mmio, chan->ddc_base, SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK);
52
	i810_readl(mmio, chan->ddc_base);	/* flush posted write */
53 54 55 56
}

static void i810i2c_setsda(void *data, int state)
{
57
        struct i810fb_i2c_chan    *chan = data;
58
        struct i810fb_par         *par = chan->par;
A
Al Viro 已提交
59
	u8                        __iomem *mmio = par->mmio_start_virtual;
60

61 62 63 64
	if (state)
		i810_writel(mmio, chan->ddc_base, SDA_DIR_MASK | SDA_VAL_MASK);
	else
		i810_writel(mmio, chan->ddc_base, SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK);
65
	i810_readl(mmio, chan->ddc_base);	/* flush posted write */
66 67 68 69
}

static int i810i2c_getscl(void *data)
{
70
        struct i810fb_i2c_chan    *chan = data;
71
        struct i810fb_par         *par = chan->par;
A
Al Viro 已提交
72
	u8                        __iomem *mmio = par->mmio_start_virtual;
73

74 75 76
	i810_writel(mmio, chan->ddc_base, SCL_DIR_MASK);
	i810_writel(mmio, chan->ddc_base, 0);
	return ((i810_readl(mmio, chan->ddc_base) & SCL_VAL_IN) != 0);
77 78 79 80
}

static int i810i2c_getsda(void *data)
{
81
        struct i810fb_i2c_chan    *chan = data;
82
        struct i810fb_par         *par = chan->par;
A
Al Viro 已提交
83
	u8                        __iomem *mmio = par->mmio_start_virtual;
84

85 86 87
	i810_writel(mmio, chan->ddc_base, SDA_DIR_MASK);
	i810_writel(mmio, chan->ddc_base, 0);
	return ((i810_readl(mmio, chan->ddc_base) & SDA_VAL_IN) != 0);
88 89
}

90
static int i810_setup_i2c_bus(struct i810fb_i2c_chan *chan, const char *name)
91 92 93 94 95 96 97
{
        int rc;

        strcpy(chan->adapter.name, name);
        chan->adapter.owner             = THIS_MODULE;
        chan->adapter.algo_data         = &chan->algo;
        chan->adapter.dev.parent        = &chan->par->dev->dev;
98 99 100 101
	chan->algo.setsda               = i810i2c_setsda;
	chan->algo.setscl               = i810i2c_setscl;
	chan->algo.getsda               = i810i2c_getsda;
	chan->algo.getscl               = i810i2c_getscl;
102 103 104 105 106 107 108 109 110 111 112 113
	chan->algo.udelay               = 10;
        chan->algo.timeout              = (HZ/2);
        chan->algo.data                 = chan;

        i2c_set_adapdata(&chan->adapter, chan);

        /* Raise SCL and SDA */
        chan->algo.setsda(chan, 1);
        chan->algo.setscl(chan, 1);
        udelay(20);

        rc = i2c_bit_add_bus(&chan->adapter);
114

115 116
        if (rc == 0)
                dev_dbg(&chan->par->dev->dev, "I2C bus %s registered.\n",name);
117
        else {
118 119
                dev_warn(&chan->par->dev->dev, "Failed to register I2C bus "
			 "%s.\n", name);
120 121 122
		chan->par = NULL;
	}

123 124 125 126 127 128 129
        return rc;
}

void i810_create_i2c_busses(struct i810fb_par *par)
{
        par->chan[0].par        = par;
	par->chan[1].par        = par;
130 131 132 133 134 135 136 137
	par->chan[2].par        = par;

	par->chan[0].ddc_base = GPIOA;
	i810_setup_i2c_bus(&par->chan[0], "I810-DDC");
	par->chan[1].ddc_base = GPIOB;
	i810_setup_i2c_bus(&par->chan[1], "I810-I2C");
	par->chan[2].ddc_base = GPIOC;
	i810_setup_i2c_bus(&par->chan[2], "I810-GPIOC");
138 139 140 141 142
}

void i810_delete_i2c_busses(struct i810fb_par *par)
{
        if (par->chan[0].par)
143
		i2c_del_adapter(&par->chan[0].adapter);
144
        par->chan[0].par = NULL;
145

146
	if (par->chan[1].par)
147
		i2c_del_adapter(&par->chan[1].adapter);
148
	par->chan[1].par = NULL;
149 150

	if (par->chan[2].par)
151
		i2c_del_adapter(&par->chan[2].adapter);
152
	par->chan[2].par = NULL;
153 154 155 156 157 158 159
}

int i810_probe_i2c_connector(struct fb_info *info, u8 **out_edid, int conn)
{
	struct i810fb_par *par = info->par;
        u8 *edid = NULL;

160 161
	DPRINTK("i810-i2c: Probe DDC%i Bus\n", conn+1);
	if (conn < par->ddc_num) {
162
		edid = fb_ddc_read(&par->chan[conn].adapter);
163
	} else {
164 165 166 167
		const u8 *e = fb_firmware_edid(info->device);

		if (e != NULL) {
			DPRINTK("i810-i2c: Getting EDID from BIOS\n");
168
			edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL);
169
		}
170 171
	}

172
	*out_edid = edid;
173 174 175

        return (edid) ? 0 : 1;
}