blob: b3065492bb20474ff3478e697702543ae474ce1e [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
Krzysztof Halasa689c9562007-10-16 01:29:31 -070061 OUTREG(chan->reg, (state ? SCL_VAL_OUT : 0) |
62 SCL_DIR | SCL_DIR_MASK | SCL_VAL_MASK);
Dennis Munsie41c94802006-06-20 14:55:55 -040063 val = INREG(chan->reg);
64}
65
66static 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
Krzysztof Halasa689c9562007-10-16 01:29:31 -070072 OUTREG(chan->reg, (state ? SDA_VAL_OUT : 0) |
73 SDA_DIR | SDA_DIR_MASK | SDA_VAL_MASK);
Dennis Munsie41c94802006-06-20 14:55:55 -040074 val = INREG(chan->reg);
75}
76
77static int intelfb_gpio_getscl(void *data)
78{
79 struct intelfb_i2c_chan *chan = data;
80 struct intelfb_info *dinfo = chan->dinfo;
81 u32 val;
82
83 OUTREG(chan->reg, SCL_DIR_MASK);
84 OUTREG(chan->reg, 0);
85 val = INREG(chan->reg);
86 return ((val & SCL_VAL_IN) != 0);
87}
88
89static int intelfb_gpio_getsda(void *data)
90{
91 struct intelfb_i2c_chan *chan = data;
92 struct intelfb_info *dinfo = chan->dinfo;
93 u32 val;
94
95 OUTREG(chan->reg, SDA_DIR_MASK);
96 OUTREG(chan->reg, 0);
97 val = INREG(chan->reg);
98 return ((val & SDA_VAL_IN) != 0);
99}
100
101static int intelfb_setup_i2c_bus(struct intelfb_info *dinfo,
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700102 struct intelfb_i2c_chan *chan,
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200103 const u32 reg, const char *name,
104 int class)
Dennis Munsie41c94802006-06-20 14:55:55 -0400105{
106 int rc;
107
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700108 chan->dinfo = dinfo;
109 chan->reg = reg;
David Brownell2096b952007-05-01 23:26:28 +0200110 snprintf(chan->adapter.name, sizeof(chan->adapter.name),
111 "intelfb %s", name);
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200112 chan->adapter.class = class;
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700113 chan->adapter.owner = THIS_MODULE;
Dennis Munsie41c94802006-06-20 14:55:55 -0400114 chan->adapter.algo_data = &chan->algo;
115 chan->adapter.dev.parent = &chan->dinfo->pdev->dev;
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700116 chan->algo.setsda = intelfb_gpio_setsda;
117 chan->algo.setscl = intelfb_gpio_setscl;
118 chan->algo.getsda = intelfb_gpio_getsda;
119 chan->algo.getscl = intelfb_gpio_getscl;
120 chan->algo.udelay = 40;
121 chan->algo.timeout = 20;
122 chan->algo.data = chan;
Dennis Munsie41c94802006-06-20 14:55:55 -0400123
124 i2c_set_adapdata(&chan->adapter, chan);
125
126 /* Raise SCL and SDA */
127 intelfb_gpio_setsda(chan, 1);
128 intelfb_gpio_setscl(chan, 1);
129 udelay(20);
130
131 rc = i2c_bit_add_bus(&chan->adapter);
132 if (rc == 0)
133 DBG_MSG("I2C bus %s registered.\n", name);
134 else
135 WRN_MSG("Failed to register I2C bus %s.\n", name);
136 return rc;
137}
138
139void intelfb_create_i2c_busses(struct intelfb_info *dinfo)
140{
141 int i = 0;
142
143 /* everyone has at least a single analog output */
144 dinfo->num_outputs = 1;
145 dinfo->output[i].type = INTELFB_OUTPUT_ANALOG;
146
147 /* setup the DDC bus for analog output */
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700148 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].ddc_bus, GPIOA,
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200149 "CRTDDC_A", I2C_CLASS_DDC);
Dennis Munsie41c94802006-06-20 14:55:55 -0400150 i++;
151
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700152 /* need to add the output busses for each device
153 - this function is very incomplete
154 - i915GM has LVDS and TVOUT for example
155 */
156 switch(dinfo->chipset) {
Dennis Munsie41c94802006-06-20 14:55:55 -0400157 case INTEL_830M:
158 case INTEL_845G:
159 case INTEL_855GM:
160 case INTEL_865G:
161 dinfo->output[i].type = INTELFB_OUTPUT_DVO;
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700162 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].ddc_bus,
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200163 GPIOD, "DVODDC_D", I2C_CLASS_DDC);
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700164 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].i2c_bus,
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200165 GPIOE, "DVOI2C_E", 0);
Dennis Munsie41c94802006-06-20 14:55:55 -0400166 i++;
167 break;
168 case INTEL_915G:
169 case INTEL_915GM:
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700170 /* has some LVDS + tv-out */
Dennis Munsie41c94802006-06-20 14:55:55 -0400171 case INTEL_945G:
172 case INTEL_945GM:
Phil Endecott3f7a26b2008-10-15 22:03:35 -0700173 case INTEL_945GME:
Maik Broemme0e170c72008-04-28 02:15:43 -0700174 case INTEL_965G:
175 case INTEL_965GM:
Dennis Munsie41c94802006-06-20 14:55:55 -0400176 /* SDVO ports have a single control bus - 2 devices */
177 dinfo->output[i].type = INTELFB_OUTPUT_SDVO;
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700178 intelfb_setup_i2c_bus(dinfo, &dinfo->output[i].i2c_bus,
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200179 GPIOE, "SDVOCTRL_E", 0);
Dennis Munsie41c94802006-06-20 14:55:55 -0400180 /* TODO: initialize the SDVO */
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700181 /* I830SDVOInit(pScrn, i, DVOB); */
Dennis Munsie41c94802006-06-20 14:55:55 -0400182 i++;
183
184 /* set up SDVOC */
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 */
Krzysztof Halasa689c9562007-10-16 01:29:31 -0700188 /* I830SDVOInit(pScrn, i, DVOC); */
Dennis Munsie41c94802006-06-20 14:55:55 -0400189 i++;
190 break;
191 }
192 dinfo->num_outputs = i;
193}
Dennis Munsie76278992006-06-20 14:55:55 -0400194
195void intelfb_delete_i2c_busses(struct intelfb_info *dinfo)
196{
197 int i;
198
199 for (i = 0; i < MAX_OUTPUTS; i++) {
200 if (dinfo->output[i].i2c_bus.dinfo) {
Jean Delvare32697112006-12-10 21:21:33 +0100201 i2c_del_adapter(&dinfo->output[i].i2c_bus.adapter);
Dennis Munsie76278992006-06-20 14:55:55 -0400202 dinfo->output[i].i2c_bus.dinfo = NULL;
203 }
204 if (dinfo->output[i].ddc_bus.dinfo) {
Jean Delvare32697112006-12-10 21:21:33 +0100205 i2c_del_adapter(&dinfo->output[i].ddc_bus.adapter);
Dennis Munsie76278992006-06-20 14:55:55 -0400206 dinfo->output[i].ddc_bus.dinfo = NULL;
207 }
208 }
209}