blob: 6fd7cb8f9b8e421a3ae65df7547b3766aeef3112 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/video/nvidia/nvidia-i2c.c - nVidia i2c
3 *
4 * Copyright 2004 Antonino A. Daplas <adaplas @pol.net>
5 *
6 * Based on rivafb-i2c.c
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive
10 * for more details.
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/delay.h>
16#include <linux/pci.h>
17#include <linux/fb.h>
18
19#include <asm/io.h>
20
21#include "nv_type.h"
22#include "nv_local.h"
23#include "nv_proto.h"
24
25#include "../edid.h"
26
27static void nvidia_gpio_setscl(void *data, int state)
28{
29 struct nvidia_i2c_chan *chan = data;
30 struct nvidia_par *par = chan->par;
31 u32 val;
32
Antonino A. Daplase2969272007-05-08 00:38:24 -070033 val = NVReadCrtc(par, chan->ddc_base + 1) & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35 if (state)
36 val |= 0x20;
37 else
38 val &= ~0x20;
39
Antonino A. Daplase2969272007-05-08 00:38:24 -070040 NVWriteCrtc(par, chan->ddc_base + 1, val | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
43static void nvidia_gpio_setsda(void *data, int state)
44{
Antonino A. Daplasc439e342006-01-09 20:53:02 -080045 struct nvidia_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct nvidia_par *par = chan->par;
47 u32 val;
48
Antonino A. Daplase2969272007-05-08 00:38:24 -070049 val = NVReadCrtc(par, chan->ddc_base + 1) & 0xf0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 if (state)
52 val |= 0x10;
53 else
54 val &= ~0x10;
55
Antonino A. Daplase2969272007-05-08 00:38:24 -070056 NVWriteCrtc(par, chan->ddc_base + 1, val | 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59static int nvidia_gpio_getscl(void *data)
60{
Antonino A. Daplasc439e342006-01-09 20:53:02 -080061 struct nvidia_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct nvidia_par *par = chan->par;
63 u32 val = 0;
64
Antonino A. Daplase2969272007-05-08 00:38:24 -070065 if (NVReadCrtc(par, chan->ddc_base) & 0x04)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 val = 1;
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return val;
69}
70
71static int nvidia_gpio_getsda(void *data)
72{
Antonino A. Daplasc439e342006-01-09 20:53:02 -080073 struct nvidia_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 struct nvidia_par *par = chan->par;
75 u32 val = 0;
76
Antonino A. Daplase2969272007-05-08 00:38:24 -070077 if (NVReadCrtc(par, chan->ddc_base) & 0x08)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 val = 1;
79
80 return val;
81}
82
Jean Delvare1e73db22007-05-08 00:38:15 -070083static int nvidia_setup_i2c_bus(struct nvidia_i2c_chan *chan, const char *name,
84 unsigned int i2c_class)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 int rc;
87
88 strcpy(chan->adapter.name, name);
89 chan->adapter.owner = THIS_MODULE;
Jean Delvare1684a9842005-08-11 23:51:10 +020090 chan->adapter.id = I2C_HW_B_NVIDIA;
Jean Delvare1e73db22007-05-08 00:38:15 -070091 chan->adapter.class = i2c_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 chan->adapter.algo_data = &chan->algo;
93 chan->adapter.dev.parent = &chan->par->pci_dev->dev;
94 chan->algo.setsda = nvidia_gpio_setsda;
95 chan->algo.setscl = nvidia_gpio_setscl;
96 chan->algo.getsda = nvidia_gpio_getsda;
97 chan->algo.getscl = nvidia_gpio_getscl;
98 chan->algo.udelay = 40;
99 chan->algo.timeout = msecs_to_jiffies(2);
100 chan->algo.data = chan;
101
102 i2c_set_adapdata(&chan->adapter, chan);
103
104 /* Raise SCL and SDA */
105 nvidia_gpio_setsda(chan, 1);
106 nvidia_gpio_setscl(chan, 1);
107 udelay(20);
108
109 rc = i2c_bit_add_bus(&chan->adapter);
110 if (rc == 0)
111 dev_dbg(&chan->par->pci_dev->dev,
112 "I2C bus %s registered.\n", name);
113 else {
114 dev_warn(&chan->par->pci_dev->dev,
115 "Failed to register I2C bus %s.\n", name);
116 chan->par = NULL;
117 }
118
119 return rc;
120}
121
122void nvidia_create_i2c_busses(struct nvidia_par *par)
123{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 par->chan[0].par = par;
125 par->chan[1].par = par;
126 par->chan[2].par = par;
127
Antonino A. Daplas3c03ec22007-10-16 01:29:20 -0700128 par->chan[0].ddc_base = (par->reverse_i2c) ? 0x36 : 0x3e;
Antonino A. Daplas076a7dce2007-10-16 01:29:36 -0700129 nvidia_setup_i2c_bus(&par->chan[0], "nvidia #0",
130 (par->reverse_i2c) ? I2C_CLASS_HWMON : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Antonino A. Daplas3c03ec22007-10-16 01:29:20 -0700132 par->chan[1].ddc_base = (par->reverse_i2c) ? 0x3e : 0x36;
Antonino A. Daplas076a7dce2007-10-16 01:29:36 -0700133 nvidia_setup_i2c_bus(&par->chan[1], "nvidia #1",
134 (par->reverse_i2c) ? 0 : I2C_CLASS_HWMON);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 par->chan[2].ddc_base = 0x50;
Jean Delvare1e73db22007-05-08 00:38:15 -0700137 nvidia_setup_i2c_bus(&par->chan[2], "nvidia #2", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140void nvidia_delete_i2c_busses(struct nvidia_par *par)
141{
Jean Delvarea65ff762007-05-08 00:38:20 -0700142 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jean Delvarea65ff762007-05-08 00:38:20 -0700144 for (i = 0; i < 3; i++) {
145 if (!par->chan[i].par)
146 continue;
147 i2c_del_adapter(&par->chan[i].adapter);
148 par->chan[i].par = NULL;
149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Antonino A. Daplas094bb652005-09-09 13:04:36 -0700152int nvidia_probe_i2c_connector(struct fb_info *info, int conn, u8 **out_edid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Antonino A. Daplas094bb652005-09-09 13:04:36 -0700154 struct nvidia_par *par = info->par;
Andrew Morton7e4910922006-10-11 01:20:35 -0700155 u8 *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Antonino A. Daplas66fd1412007-05-08 00:37:27 -0700157 if (par->chan[conn - 1].par)
158 edid = fb_ddc_read(&par->chan[conn - 1].adapter);
Antonino A. Daplas094bb652005-09-09 13:04:36 -0700159
160 if (!edid && conn == 1) {
161 /* try to get from firmware */
Antonino A. Daplas0ed8e042005-09-14 14:19:15 -0700162 const u8 *e = fb_firmware_edid(info->device);
163
Alexey Dobriyanbfba7b32006-12-08 02:40:46 -0800164 if (e != NULL)
165 edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL);
Antonino A. Daplas094bb652005-09-09 13:04:36 -0700166 }
167
Antonino A. Daplas3d5b1912006-03-27 01:17:33 -0800168 *out_edid = edid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Antonino A. Daplas094bb652005-09-09 13:04:36 -0700170 return (edid) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}