blob: c1f7b49975dd1a8bb78fd1817ddfd33cd3e5018a [file] [log] [blame]
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -07001 /*-*- linux-c -*-
2 * linux/drivers/video/i810-i2c.c -- Intel 810/815 I2C support
3 *
4 * Copyright (C) 2004 Antonino Daplas<adaplas@pol.net>
5 * All Rights Reserved
6 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file COPYING in the main directory of this archive for
9 * more details.
10 */
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070011#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/sched.h>
14#include <linux/delay.h>
15#include <linux/pci.h>
16#include <linux/fb.h>
17#include "i810.h"
18#include "i810_regs.h"
Adrian Bunka0aa7d02006-01-09 20:54:04 -080019#include "i810_main.h"
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070020#include "../edid.h"
21
22#define I810_DDC 0x50
23/* bit locations in the registers */
24#define SCL_DIR_MASK 0x0001
25#define SCL_DIR 0x0002
26#define SCL_VAL_MASK 0x0004
27#define SCL_VAL_OUT 0x0008
28#define SCL_VAL_IN 0x0010
29#define SDA_DIR_MASK 0x0100
30#define SDA_DIR 0x0200
31#define SDA_VAL_MASK 0x0400
32#define SDA_VAL_OUT 0x0800
33#define SDA_VAL_IN 0x1000
34
35#define DEBUG /* define this for verbose EDID parsing output */
36
37#ifdef DEBUG
38#define DPRINTK(fmt, args...) printk(fmt,## args)
39#else
40#define DPRINTK(fmt, args...)
41#endif
42
43static void i810i2c_setscl(void *data, int state)
44{
Antonino A. Daplasc019c0e2006-01-09 20:53:03 -080045 struct i810fb_i2c_chan *chan = data;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070046 struct i810fb_par *par = chan->par;
Al Virobe88ec72005-09-29 00:36:10 +010047 u8 __iomem *mmio = par->mmio_start_virtual;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070048
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080049 i810_writel(mmio, chan->ddc_base, (state ? SCL_VAL_OUT : 0) | SCL_DIR |
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070050 SCL_DIR_MASK | SCL_VAL_MASK);
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080051 i810_readl(mmio, chan->ddc_base); /* flush posted write */
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070052}
53
54static void i810i2c_setsda(void *data, int state)
55{
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080056 struct i810fb_i2c_chan *chan = data;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070057 struct i810fb_par *par = chan->par;
Al Virobe88ec72005-09-29 00:36:10 +010058 u8 __iomem *mmio = par->mmio_start_virtual;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070059
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080060 i810_writel(mmio, chan->ddc_base, (state ? SDA_VAL_OUT : 0) | SDA_DIR |
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070061 SDA_DIR_MASK | SDA_VAL_MASK);
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080062 i810_readl(mmio, chan->ddc_base); /* flush posted write */
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070063}
64
65static int i810i2c_getscl(void *data)
66{
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080067 struct i810fb_i2c_chan *chan = data;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070068 struct i810fb_par *par = chan->par;
Al Virobe88ec72005-09-29 00:36:10 +010069 u8 __iomem *mmio = par->mmio_start_virtual;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070070
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080071 i810_writel(mmio, chan->ddc_base, SCL_DIR_MASK);
72 i810_writel(mmio, chan->ddc_base, 0);
73 return ((i810_readl(mmio, chan->ddc_base) & SCL_VAL_IN) != 0);
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070074}
75
76static int i810i2c_getsda(void *data)
77{
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080078 struct i810fb_i2c_chan *chan = data;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070079 struct i810fb_par *par = chan->par;
Al Virobe88ec72005-09-29 00:36:10 +010080 u8 __iomem *mmio = par->mmio_start_virtual;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070081
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080082 i810_writel(mmio, chan->ddc_base, SDA_DIR_MASK);
83 i810_writel(mmio, chan->ddc_base, 0);
84 return ((i810_readl(mmio, chan->ddc_base) & SDA_VAL_IN) != 0);
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070085}
86
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080087static int i810_setup_i2c_bus(struct i810fb_i2c_chan *chan, const char *name)
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -070088{
89 int rc;
90
91 strcpy(chan->adapter.name, name);
92 chan->adapter.owner = THIS_MODULE;
93 chan->adapter.algo_data = &chan->algo;
94 chan->adapter.dev.parent = &chan->par->dev->dev;
Antonino A. Daplas5fab8512005-11-07 01:00:50 -080095 chan->adapter.id = I2C_HW_B_I810;
96 chan->algo.setsda = i810i2c_setsda;
97 chan->algo.setscl = i810i2c_setscl;
98 chan->algo.getsda = i810i2c_getsda;
99 chan->algo.getscl = i810i2c_getscl;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700100 chan->algo.udelay = 10;
101 chan->algo.mdelay = 10;
102 chan->algo.timeout = (HZ/2);
103 chan->algo.data = chan;
104
105 i2c_set_adapdata(&chan->adapter, chan);
106
107 /* Raise SCL and SDA */
108 chan->algo.setsda(chan, 1);
109 chan->algo.setscl(chan, 1);
110 udelay(20);
111
112 rc = i2c_bit_add_bus(&chan->adapter);
Antonino A. Daplas5fab8512005-11-07 01:00:50 -0800113
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700114 if (rc == 0)
115 dev_dbg(&chan->par->dev->dev, "I2C bus %s registered.\n",name);
Antonino A. Daplas5fab8512005-11-07 01:00:50 -0800116 else {
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700117 dev_warn(&chan->par->dev->dev, "Failed to register I2C bus "
118 "%s.\n", name);
Antonino A. Daplas5fab8512005-11-07 01:00:50 -0800119 chan->par = NULL;
120 }
121
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700122 return rc;
123}
124
125void i810_create_i2c_busses(struct i810fb_par *par)
126{
127 par->chan[0].par = par;
128 par->chan[1].par = par;
Antonino A. Daplas5fab8512005-11-07 01:00:50 -0800129 par->chan[2].par = par;
130
131 par->chan[0].ddc_base = GPIOA;
132 i810_setup_i2c_bus(&par->chan[0], "I810-DDC");
133 par->chan[1].ddc_base = GPIOB;
134 i810_setup_i2c_bus(&par->chan[1], "I810-I2C");
135 par->chan[2].ddc_base = GPIOC;
136 i810_setup_i2c_bus(&par->chan[2], "I810-GPIOC");
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700137}
138
139void i810_delete_i2c_busses(struct i810fb_par *par)
140{
141 if (par->chan[0].par)
142 i2c_bit_del_bus(&par->chan[0].adapter);
143 par->chan[0].par = NULL;
Antonino A. Daplas5fab8512005-11-07 01:00:50 -0800144
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700145 if (par->chan[1].par)
146 i2c_bit_del_bus(&par->chan[1].adapter);
147 par->chan[1].par = NULL;
Antonino A. Daplas5fab8512005-11-07 01:00:50 -0800148
149 if (par->chan[2].par)
150 i2c_bit_del_bus(&par->chan[2].adapter);
151 par->chan[2].par = NULL;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700152}
153
154static u8 *i810_do_probe_i2c_edid(struct i810fb_i2c_chan *chan)
155{
156 u8 start = 0x0;
157 struct i2c_msg msgs[] = {
158 {
159 .addr = I810_DDC,
160 .len = 1,
161 .buf = &start,
162 }, {
163 .addr = I810_DDC,
164 .flags = I2C_M_RD,
165 .len = EDID_LENGTH,
166 },
167 };
168 u8 *buf;
169
170 buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
171 if (!buf) {
172 DPRINTK("i810-i2c: Failed to allocate memory\n");
173 return NULL;
174 }
175 msgs[1].buf = buf;
176
177 if (i2c_transfer(&chan->adapter, msgs, 2) == 2) {
178 DPRINTK("i810-i2c: I2C Transfer successful\n");
179 return buf;
180 }
Antonino A. Daplas5fab8512005-11-07 01:00:50 -0800181
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700182 DPRINTK("i810-i2c: Unable to read EDID block.\n");
183 kfree(buf);
184 return NULL;
185}
186
187int i810_probe_i2c_connector(struct fb_info *info, u8 **out_edid, int conn)
188{
189 struct i810fb_par *par = info->par;
190 u8 *edid = NULL;
191 int i;
192
Manuel Lauss00d340b92006-02-01 03:06:54 -0800193 DPRINTK("i810-i2c: Probe DDC%i Bus\n", conn+1);
194 if (conn < par->ddc_num) {
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700195 for (i = 0; i < 3; i++) {
196 /* Do the real work */
Manuel Lauss00d340b92006-02-01 03:06:54 -0800197 edid = i810_do_probe_i2c_edid(&par->chan[conn]);
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700198 if (edid)
199 break;
200 }
201 } else {
Antonino A. Daplas5fab8512005-11-07 01:00:50 -0800202 const u8 *e = fb_firmware_edid(info->device);
203
204 if (e != NULL) {
205 DPRINTK("i810-i2c: Getting EDID from BIOS\n");
206 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
207 if (edid)
208 memcpy(edid, e, EDID_LENGTH);
209 }
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700210 }
211
Antonino A. Daplas7c518eb2006-03-27 01:17:33 -0800212 *out_edid = edid;
Antonino A. Daplas74f6ae82005-09-09 13:10:04 -0700213
214 return (edid) ? 0 : 1;
215}