Pull video into test branch
[linux-2.6] / drivers / video / intelfb / intelfb_i2c.c
1 /**************************************************************************
2
3  Copyright 2006 Dave Airlie <airlied@linux.ie>
4
5 All Rights Reserved.
6
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:
13
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
16 Software.
17
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.
25
26 **************************************************************************/
27
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/delay.h>
32 #include <linux/pci.h>
33 #include <linux/fb.h>
34
35 #include <linux/i2c.h>
36 #include <linux/i2c-id.h>
37 #include <linux/i2c-algo-bit.h>
38
39 #include <asm/io.h>
40
41 #include "intelfb.h"
42 #include "intelfbhw.h"
43
44 /* bit locations in the registers */
45 #define SCL_DIR_MASK            0x0001
46 #define SCL_DIR                 0x0002
47 #define SCL_VAL_MASK            0x0004
48 #define SCL_VAL_OUT             0x0008
49 #define SCL_VAL_IN              0x0010
50 #define SDA_DIR_MASK            0x0100
51 #define SDA_DIR                 0x0200
52 #define SDA_VAL_MASK            0x0400
53 #define SDA_VAL_OUT             0x0800
54 #define SDA_VAL_IN              0x1000
55
56 static void intelfb_gpio_setscl(void *data, int state)
57 {
58         struct intelfb_i2c_chan *chan = data;
59         struct intelfb_info *dinfo = chan->dinfo;
60         u32 val;
61
62         OUTREG(chan->reg, (state ? SCL_VAL_OUT : 0) | SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK);
63         val = INREG(chan->reg);
64 }
65
66 static void intelfb_gpio_setsda(void *data, int state)
67 {
68         struct intelfb_i2c_chan *chan = data;
69         struct intelfb_info *dinfo = chan->dinfo;
70         u32 val;
71
72         OUTREG(chan->reg, (state ? SDA_VAL_OUT : 0) | SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK);
73         val = INREG(chan->reg);
74 }
75
76 static int intelfb_gpio_getscl(void *data)
77 {
78         struct intelfb_i2c_chan *chan = data;
79         struct intelfb_info *dinfo = chan->dinfo;
80         u32 val;
81
82         OUTREG(chan->reg, SCL_DIR_MASK);
83         OUTREG(chan->reg, 0);
84         val = INREG(chan->reg);
85         return ((val & SCL_VAL_IN) != 0);
86 }
87
88 static int intelfb_gpio_getsda(void *data)
89 {
90         struct intelfb_i2c_chan *chan = data;
91         struct intelfb_info *dinfo = chan->dinfo;
92         u32 val;
93
94         OUTREG(chan->reg, SDA_DIR_MASK);
95         OUTREG(chan->reg, 0);
96         val = INREG(chan->reg);
97         return ((val & SDA_VAL_IN) != 0);
98 }
99
100 static int intelfb_setup_i2c_bus(struct intelfb_info *dinfo,
101                                                                  struct intelfb_i2c_chan *chan,
102                                                                  const u32 reg, const char *name)
103 {
104         int rc;
105
106         chan->dinfo                                     = dinfo;
107         chan->reg                                       = reg;
108         snprintf(chan->adapter.name, I2C_NAME_SIZE, "intelfb %s", name);
109         chan->adapter.owner                     = THIS_MODULE;
110         chan->adapter.id                        = I2C_HW_B_INTELFB;
111         chan->adapter.algo_data         = &chan->algo;
112         chan->adapter.dev.parent        = &chan->dinfo->pdev->dev;
113         chan->algo.setsda                       = intelfb_gpio_setsda;
114         chan->algo.setscl                       = intelfb_gpio_setscl;
115         chan->algo.getsda                       = intelfb_gpio_getsda;
116         chan->algo.getscl                       = intelfb_gpio_getscl;
117         chan->algo.udelay                       = 40;
118         chan->algo.timeout                      = 20;
119         chan->algo.data                         = chan;
120
121         i2c_set_adapdata(&chan->adapter, chan);
122
123         /* Raise SCL and SDA */
124         intelfb_gpio_setsda(chan, 1);
125         intelfb_gpio_setscl(chan, 1);
126         udelay(20);
127
128         rc = i2c_bit_add_bus(&chan->adapter);
129         if (rc == 0)
130                 DBG_MSG("I2C bus %s registered.\n", name);
131         else
132                 WRN_MSG("Failed to register I2C bus %s.\n", name);
133         return rc;
134 }
135
136 void intelfb_create_i2c_busses(struct intelfb_info *dinfo)
137 {
138         int i = 0;
139
140         /* everyone has at least a single analog output */
141         dinfo->num_outputs = 1;
142         dinfo->output[i].type = INTELFB_OUTPUT_ANALOG;
143
144         /* setup the DDC bus for analog output */
145         intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].ddc_bus, GPIOA, "CRTDDC_A");
146         i++;
147
148     /* need to add the output busses for each device
149        - this function is very incomplete
150        - i915GM has LVDS and TVOUT for example
151     */
152     switch(dinfo->chipset) {
153         case INTEL_830M:
154         case INTEL_845G:
155         case INTEL_855GM:
156         case INTEL_865G:
157                 dinfo->output[i].type = INTELFB_OUTPUT_DVO;
158                 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].ddc_bus, GPIOD, "DVODDC_D");
159                 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].i2c_bus, GPIOE, "DVOI2C_E");
160                 i++;
161                 break;
162         case INTEL_915G:
163         case INTEL_915GM:
164                 /* has  some LVDS + tv-out */
165         case INTEL_945G:
166         case INTEL_945GM:
167                 /* SDVO ports have a single control bus - 2 devices */
168                 dinfo->output[i].type = INTELFB_OUTPUT_SDVO;
169                 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].i2c_bus, GPIOE, "SDVOCTRL_E");
170                 /* TODO: initialize the SDVO */
171 //              I830SDVOInit(pScrn, i, DVOB);
172                 i++;
173
174                 /* set up SDVOC */
175                 dinfo->output[i].type = INTELFB_OUTPUT_SDVO;
176                 dinfo->output[i].i2c_bus = dinfo->output[i - 1].i2c_bus;
177                 /* TODO: initialize the SDVO */
178 //              I830SDVOInit(pScrn, i, DVOC);
179                 i++;
180                 break;
181         }
182         dinfo->num_outputs = i;
183 }
184
185 void intelfb_delete_i2c_busses(struct intelfb_info *dinfo)
186 {
187         int i;
188
189         for (i = 0; i < MAX_OUTPUTS; i++) {
190                 if (dinfo->output[i].i2c_bus.dinfo) {
191                         i2c_del_adapter(&dinfo->output[i].i2c_bus.adapter);
192                         dinfo->output[i].i2c_bus.dinfo = NULL;
193                 }
194                 if (dinfo->output[i].ddc_bus.dinfo) {
195                         i2c_del_adapter(&dinfo->output[i].ddc_bus.adapter);
196                         dinfo->output[i].ddc_bus.dinfo = NULL;
197                 }
198         }
199 }