intelfb_i2c.c 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/**************************************************************************

 Copyright 2006 Dave Airlie <airlied@linux.ie>

All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
on the rights to use, copy, modify, merge, publish, distribute, sub
license, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
THE COPYRIGHT HOLDERS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/pci.h>
#include <linux/fb.h>

#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>

#include <asm/io.h>

#include "intelfb.h"
#include "intelfbhw.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

static void intelfb_gpio_setscl(void *data, int state)
{
	struct intelfb_i2c_chan *chan = data;
	struct intelfb_info *dinfo = chan->dinfo;
	u32 val;

60 61
	OUTREG(chan->reg, (state ? SCL_VAL_OUT : 0) |
	       SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK);
62 63 64 65 66 67 68 69 70
	val = INREG(chan->reg);
}

static void intelfb_gpio_setsda(void *data, int state)
{
	struct intelfb_i2c_chan *chan = data;
	struct intelfb_info *dinfo = chan->dinfo;
	u32 val;

71 72
	OUTREG(chan->reg, (state ? SDA_VAL_OUT : 0) |
	       SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	val = INREG(chan->reg);
}

static int intelfb_gpio_getscl(void *data)
{
	struct intelfb_i2c_chan *chan = data;
	struct intelfb_info *dinfo = chan->dinfo;
	u32 val;

	OUTREG(chan->reg, SCL_DIR_MASK);
	OUTREG(chan->reg, 0);
	val = INREG(chan->reg);
	return ((val & SCL_VAL_IN) != 0);
}

static int intelfb_gpio_getsda(void *data)
{
	struct intelfb_i2c_chan *chan = data;
	struct intelfb_info *dinfo = chan->dinfo;
	u32 val;

	OUTREG(chan->reg, SDA_DIR_MASK);
	OUTREG(chan->reg, 0);
	val = INREG(chan->reg);
	return ((val & SDA_VAL_IN) != 0);
}

static int intelfb_setup_i2c_bus(struct intelfb_info *dinfo,
101
				 struct intelfb_i2c_chan *chan,
102 103
				 const u32 reg, const char *name,
				 int class)
104 105 106
{
	int rc;

107 108
	chan->dinfo			= dinfo;
	chan->reg			= reg;
D
David Brownell 已提交
109 110
	snprintf(chan->adapter.name, sizeof(chan->adapter.name),
		 "intelfb %s", name);
111
	chan->adapter.class		= class;
112
	chan->adapter.owner		= THIS_MODULE;
113 114
	chan->adapter.algo_data		= &chan->algo;
	chan->adapter.dev.parent	= &chan->dinfo->pdev->dev;
115 116 117 118 119 120 121
	chan->algo.setsda		= intelfb_gpio_setsda;
	chan->algo.setscl		= intelfb_gpio_setscl;
	chan->algo.getsda		= intelfb_gpio_getsda;
	chan->algo.getscl		= intelfb_gpio_getscl;
	chan->algo.udelay		= 40;
	chan->algo.timeout		= 20;
	chan->algo.data			= chan;
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

	i2c_set_adapdata(&chan->adapter, chan);

	/* Raise SCL and SDA */
	intelfb_gpio_setsda(chan, 1);
	intelfb_gpio_setscl(chan, 1);
	udelay(20);

	rc = i2c_bit_add_bus(&chan->adapter);
	if (rc == 0)
		DBG_MSG("I2C bus %s registered.\n", name);
	else
		WRN_MSG("Failed to register I2C bus %s.\n", name);
	return rc;
}

void intelfb_create_i2c_busses(struct intelfb_info *dinfo)
{
	int i = 0;

	/* everyone has at least a single analog output */
	dinfo->num_outputs = 1;
	dinfo->output[i].type = INTELFB_OUTPUT_ANALOG;

	/* setup the DDC bus for analog output */
147
	intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].ddc_bus, GPIOA,
148
			      "CRTDDC_A", I2C_CLASS_DDC);
149 150
	i++;

151 152 153 154 155
	/* need to add the output busses for each device
	   - this function is very incomplete
	   - i915GM has LVDS and TVOUT for example
	*/
	switch(dinfo->chipset) {
156 157
	case INTEL_830M:
	case INTEL_845G:
S
Stefan Husemann 已提交
158
	case INTEL_854:
159 160 161
	case INTEL_855GM:
	case INTEL_865G:
		dinfo->output[i].type = INTELFB_OUTPUT_DVO;
162
		intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].ddc_bus,
163
				      GPIOD, "DVODDC_D", I2C_CLASS_DDC);
164
		intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].i2c_bus,
165
				      GPIOE, "DVOI2C_E", 0);
166 167 168 169
		i++;
		break;
	case INTEL_915G:
	case INTEL_915GM:
170
		/* has some LVDS + tv-out */
171 172
	case INTEL_945G:
	case INTEL_945GM:
173
	case INTEL_945GME:
174 175
	case INTEL_965G:
	case INTEL_965GM:
176 177
		/* SDVO ports have a single control bus - 2 devices */
		dinfo->output[i].type = INTELFB_OUTPUT_SDVO;
178
		intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].i2c_bus,
179
				      GPIOE, "SDVOCTRL_E", 0);
180
		/* TODO: initialize the SDVO */
181
		/* I830SDVOInit(pScrn, i, DVOB); */
182 183 184 185 186 187
		i++;

		/* set up SDVOC */
		dinfo->output[i].type = INTELFB_OUTPUT_SDVO;
		dinfo->output[i].i2c_bus = dinfo->output[i - 1].i2c_bus;
		/* TODO: initialize the SDVO */
188
		/* I830SDVOInit(pScrn, i, DVOC); */
189 190 191 192 193
		i++;
		break;
	}
	dinfo->num_outputs = i;
}
194 195 196 197 198 199 200

void intelfb_delete_i2c_busses(struct intelfb_info *dinfo)
{
	int i;

	for (i = 0; i < MAX_OUTPUTS; i++) {
		if (dinfo->output[i].i2c_bus.dinfo) {
201
			i2c_del_adapter(&dinfo->output[i].i2c_bus.adapter);
202 203 204
			dinfo->output[i].i2c_bus.dinfo = NULL;
		}
		if (dinfo->output[i].ddc_bus.dinfo) {
205
			i2c_del_adapter(&dinfo->output[i].ddc_bus.adapter);
206 207 208 209
			dinfo->output[i].ddc_bus.dinfo = NULL;
		}
	}
}