blob: f4ede5f6b588da583e43545566737fc736545104 [file] [log] [blame]
Dennis Munsie41c94802006-06-20 14:55:55 -04001/**************************************************************************
2
3 Copyright 2006 Dave Airlie <airlied@linux.ie>
4
5All Rights Reserved.
6
7Permission is hereby granted, free of charge, to any person obtaining a
8copy of this software and associated documentation files (the "Software"),
9to deal in the Software without restriction, including without limitation
10on the rights to use, copy, modify, merge, publish, distribute, sub
11license, and/or sell copies of the Software, and to permit persons to whom
12the Software is furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice (including the next
15paragraph) shall be included in all copies or substantial portions of the
16Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21THE COPYRIGHT HOLDERS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26**************************************************************************/
27
Dennis Munsie41c94802006-06-20 14:55:55 -040028#include <linux/module.h>
29#include <linux/kernel.h>
Dennis Munsie41c94802006-06-20 14:55:55 -040030#include <linux/delay.h>
31#include <linux/pci.h>
32#include <linux/fb.h>
33
34#include <linux/i2c.h>
35#include <linux/i2c-id.h>
36#include <linux/i2c-algo-bit.h>
37
38#include <asm/io.h>
39
40#include "intelfb.h"
41#include "intelfbhw.h"
42
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
54
55static void intelfb_gpio_setscl(void *data, int state)
56{
57 struct intelfb_i2c_chan *chan = data;
58 struct intelfb_info *dinfo = chan->dinfo;
59 u32 val;
60
61 OUTREG(chan->reg, (state ? SCL_VAL_OUT : 0) | SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK);
62 val = INREG(chan->reg);
63}
64
65static void intelfb_gpio_setsda(void *data, int state)
66{
67 struct intelfb_i2c_chan *chan = data;
68 struct intelfb_info *dinfo = chan->dinfo;
69 u32 val;
70
71 OUTREG(chan->reg, (state ? SDA_VAL_OUT : 0) | SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK);
72 val = INREG(chan->reg);
73}
74
75static int intelfb_gpio_getscl(void *data)
76{
77 struct intelfb_i2c_chan *chan = data;
78 struct intelfb_info *dinfo = chan->dinfo;
79 u32 val;
80
81 OUTREG(chan->reg, SCL_DIR_MASK);
82 OUTREG(chan->reg, 0);
83 val = INREG(chan->reg);
84 return ((val & SCL_VAL_IN) != 0);
85}
86
87static int intelfb_gpio_getsda(void *data)
88{
89 struct intelfb_i2c_chan *chan = data;
90 struct intelfb_info *dinfo = chan->dinfo;
91 u32 val;
92
93 OUTREG(chan->reg, SDA_DIR_MASK);
94 OUTREG(chan->reg, 0);
95 val = INREG(chan->reg);
96 return ((val & SDA_VAL_IN) != 0);
97}
98
99static int intelfb_setup_i2c_bus(struct intelfb_info *dinfo,
100 struct intelfb_i2c_chan *chan,
101 const u32 reg, const char *name)
102{
103 int rc;
104
105 chan->dinfo = dinfo;
106 chan->reg = reg;
107 snprintf(chan->adapter.name, I2C_NAME_SIZE, "intelfb %s", name);
108 chan->adapter.owner = THIS_MODULE;
109 chan->adapter.id = I2C_HW_B_INTELFB;
110 chan->adapter.algo_data = &chan->algo;
111 chan->adapter.dev.parent = &chan->dinfo->pdev->dev;
112 chan->algo.setsda = intelfb_gpio_setsda;
113 chan->algo.setscl = intelfb_gpio_setscl;
114 chan->algo.getsda = intelfb_gpio_getsda;
115 chan->algo.getscl = intelfb_gpio_getscl;
116 chan->algo.udelay = 40;
117 chan->algo.timeout = 20;
118 chan->algo.data = chan;
119
120 i2c_set_adapdata(&chan->adapter, chan);
121
122 /* Raise SCL and SDA */
123 intelfb_gpio_setsda(chan, 1);
124 intelfb_gpio_setscl(chan, 1);
125 udelay(20);
126
127 rc = i2c_bit_add_bus(&chan->adapter);
128 if (rc == 0)
129 DBG_MSG("I2C bus %s registered.\n", name);
130 else
131 WRN_MSG("Failed to register I2C bus %s.\n", name);
132 return rc;
133}
134
135void intelfb_create_i2c_busses(struct intelfb_info *dinfo)
136{
137 int i = 0;
138
139 /* everyone has at least a single analog output */
140 dinfo->num_outputs = 1;
141 dinfo->output[i].type = INTELFB_OUTPUT_ANALOG;
142
143 /* setup the DDC bus for analog output */
144 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].ddc_bus, GPIOA, "CRTDDC_A");
145 i++;
146
147 /* need to add the output busses for each device
148 - this function is very incomplete
149 - i915GM has LVDS and TVOUT for example
150 */
151 switch(dinfo->chipset) {
152 case INTEL_830M:
153 case INTEL_845G:
154 case INTEL_855GM:
155 case INTEL_865G:
156 dinfo->output[i].type = INTELFB_OUTPUT_DVO;
157 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].ddc_bus, GPIOD, "DVODDC_D");
158 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].i2c_bus, GPIOE, "DVOI2C_E");
159 i++;
160 break;
161 case INTEL_915G:
162 case INTEL_915GM:
163 /* has some LVDS + tv-out */
164 case INTEL_945G:
165 case INTEL_945GM:
166 /* SDVO ports have a single control bus - 2 devices */
167 dinfo->output[i].type = INTELFB_OUTPUT_SDVO;
168 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].i2c_bus, GPIOE, "SDVOCTRL_E");
169 /* TODO: initialize the SDVO */
170// I830SDVOInit(pScrn, i, DVOB);
171 i++;
172
173 /* set up SDVOC */
174 dinfo->output[i].type = INTELFB_OUTPUT_SDVO;
175 dinfo->output[i].i2c_bus = dinfo->output[i - 1].i2c_bus;
176 /* TODO: initialize the SDVO */
177// I830SDVOInit(pScrn, i, DVOC);
178 i++;
179 break;
180 }
181 dinfo->num_outputs = i;
182}
Dennis Munsie76278992006-06-20 14:55:55 -0400183
184void intelfb_delete_i2c_busses(struct intelfb_info *dinfo)
185{
186 int i;
187
188 for (i = 0; i < MAX_OUTPUTS; i++) {
189 if (dinfo->output[i].i2c_bus.dinfo) {
Jean Delvare32697112006-12-10 21:21:33 +0100190 i2c_del_adapter(&dinfo->output[i].i2c_bus.adapter);
Dennis Munsie76278992006-06-20 14:55:55 -0400191 dinfo->output[i].i2c_bus.dinfo = NULL;
192 }
193 if (dinfo->output[i].ddc_bus.dinfo) {
Jean Delvare32697112006-12-10 21:21:33 +0100194 i2c_del_adapter(&dinfo->output[i].ddc_bus.adapter);
Dennis Munsie76278992006-06-20 14:55:55 -0400195 dinfo->output[i].ddc_bus.dinfo = NULL;
196 }
197 }
198}