1 /**************************************************************************
3 Copyright 2006 Dave Airlie <airlied@linux.ie>
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 THE COPYRIGHT HOLDERS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/pci.h>
34 #include <linux/i2c.h>
35 #include <linux/i2c-id.h>
36 #include <linux/i2c-algo-bit.h>
41 #include "intelfbhw.h"
43 /* bit locations in the registers */
44 #define SCL_DIR_MASK 0x0001
45 #define SCL_DIR 0x0002
46 #define SCL_VAL_MASK 0x0004
47 #define SCL_VAL_OUT 0x0008
48 #define SCL_VAL_IN 0x0010
49 #define SDA_DIR_MASK 0x0100
50 #define SDA_DIR 0x0200
51 #define SDA_VAL_MASK 0x0400
52 #define SDA_VAL_OUT 0x0800
53 #define SDA_VAL_IN 0x1000
55 static void intelfb_gpio_setscl(void *data, int state)
57 struct intelfb_i2c_chan *chan = data;
58 struct intelfb_info *dinfo = chan->dinfo;
61 OUTREG(chan->reg, (state ? SCL_VAL_OUT : 0) |
62 SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK);
63 val = INREG(chan->reg);
66 static void intelfb_gpio_setsda(void *data, int state)
68 struct intelfb_i2c_chan *chan = data;
69 struct intelfb_info *dinfo = chan->dinfo;
72 OUTREG(chan->reg, (state ? SDA_VAL_OUT : 0) |
73 SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK);
74 val = INREG(chan->reg);
77 static int intelfb_gpio_getscl(void *data)
79 struct intelfb_i2c_chan *chan = data;
80 struct intelfb_info *dinfo = chan->dinfo;
83 OUTREG(chan->reg, SCL_DIR_MASK);
85 val = INREG(chan->reg);
86 return ((val & SCL_VAL_IN) != 0);
89 static int intelfb_gpio_getsda(void *data)
91 struct intelfb_i2c_chan *chan = data;
92 struct intelfb_info *dinfo = chan->dinfo;
95 OUTREG(chan->reg, SDA_DIR_MASK);
97 val = INREG(chan->reg);
98 return ((val & SDA_VAL_IN) != 0);
101 static int intelfb_setup_i2c_bus(struct intelfb_info *dinfo,
102 struct intelfb_i2c_chan *chan,
103 const u32 reg, const char *name,
110 snprintf(chan->adapter.name, sizeof(chan->adapter.name),
112 chan->adapter.class = class;
113 chan->adapter.owner = THIS_MODULE;
114 chan->adapter.id = I2C_HW_B_INTELFB;
115 chan->adapter.algo_data = &chan->algo;
116 chan->adapter.dev.parent = &chan->dinfo->pdev->dev;
117 chan->algo.setsda = intelfb_gpio_setsda;
118 chan->algo.setscl = intelfb_gpio_setscl;
119 chan->algo.getsda = intelfb_gpio_getsda;
120 chan->algo.getscl = intelfb_gpio_getscl;
121 chan->algo.udelay = 40;
122 chan->algo.timeout = 20;
123 chan->algo.data = chan;
125 i2c_set_adapdata(&chan->adapter, chan);
127 /* Raise SCL and SDA */
128 intelfb_gpio_setsda(chan, 1);
129 intelfb_gpio_setscl(chan, 1);
132 rc = i2c_bit_add_bus(&chan->adapter);
134 DBG_MSG("I2C bus %s registered.\n", name);
136 WRN_MSG("Failed to register I2C bus %s.\n", name);
140 void intelfb_create_i2c_busses(struct intelfb_info *dinfo)
144 /* everyone has at least a single analog output */
145 dinfo->num_outputs = 1;
146 dinfo->output[i].type = INTELFB_OUTPUT_ANALOG;
148 /* setup the DDC bus for analog output */
149 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].ddc_bus, GPIOA,
150 "CRTDDC_A", I2C_CLASS_DDC);
153 /* need to add the output busses for each device
154 - this function is very incomplete
155 - i915GM has LVDS and TVOUT for example
157 switch(dinfo->chipset) {
162 dinfo->output[i].type = INTELFB_OUTPUT_DVO;
163 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].ddc_bus,
164 GPIOD, "DVODDC_D", I2C_CLASS_DDC);
165 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].i2c_bus,
166 GPIOE, "DVOI2C_E", 0);
171 /* has some LVDS + tv-out */
176 /* SDVO ports have a single control bus - 2 devices */
177 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); */
185 dinfo->output[i].type = INTELFB_OUTPUT_SDVO;
186 dinfo->output[i].i2c_bus = dinfo->output[i - 1].i2c_bus;
187 /* TODO: initialize the SDVO */
188 /* I830SDVOInit(pScrn, i, DVOC); */
192 dinfo->num_outputs = i;
195 void intelfb_delete_i2c_busses(struct intelfb_info *dinfo)
199 for (i = 0; i < MAX_OUTPUTS; i++) {
200 if (dinfo->output[i].i2c_bus.dinfo) {
201 i2c_del_adapter(&dinfo->output[i].i2c_bus.adapter);
202 dinfo->output[i].i2c_bus.dinfo = NULL;
204 if (dinfo->output[i].ddc_bus.dinfo) {
205 i2c_del_adapter(&dinfo->output[i].ddc_bus.adapter);
206 dinfo->output[i].ddc_bus.dinfo = NULL;