]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/video/nvidia/nv_i2c.c
Merge ARM fixes
[mirror_ubuntu-bionic-kernel.git] / drivers / video / nvidia / nv_i2c.c
1 /*
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
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #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
27 static 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
33 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
34 val = VGA_RD08(par->PCIO, 0x3d5) & 0xf0;
35
36 if (state)
37 val |= 0x20;
38 else
39 val &= ~0x20;
40
41 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
42 VGA_WR08(par->PCIO, 0x3d5, val | 0x1);
43 }
44
45 static void nvidia_gpio_setsda(void *data, int state)
46 {
47 struct nvidia_i2c_chan *chan = data;
48 struct nvidia_par *par = chan->par;
49 u32 val;
50
51 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
52 val = VGA_RD08(par->PCIO, 0x3d5) & 0xf0;
53
54 if (state)
55 val |= 0x10;
56 else
57 val &= ~0x10;
58
59 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
60 VGA_WR08(par->PCIO, 0x3d5, val | 0x1);
61 }
62
63 static int nvidia_gpio_getscl(void *data)
64 {
65 struct nvidia_i2c_chan *chan = data;
66 struct nvidia_par *par = chan->par;
67 u32 val = 0;
68
69 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base);
70 if (VGA_RD08(par->PCIO, 0x3d5) & 0x04)
71 val = 1;
72
73 val = VGA_RD08(par->PCIO, 0x3d5);
74
75 return val;
76 }
77
78 static int nvidia_gpio_getsda(void *data)
79 {
80 struct nvidia_i2c_chan *chan = data;
81 struct nvidia_par *par = chan->par;
82 u32 val = 0;
83
84 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base);
85 if (VGA_RD08(par->PCIO, 0x3d5) & 0x08)
86 val = 1;
87
88 return val;
89 }
90
91 static int nvidia_setup_i2c_bus(struct nvidia_i2c_chan *chan, const char *name)
92 {
93 int rc;
94
95 strcpy(chan->adapter.name, name);
96 chan->adapter.owner = THIS_MODULE;
97 chan->adapter.id = I2C_HW_B_NVIDIA;
98 chan->adapter.algo_data = &chan->algo;
99 chan->adapter.dev.parent = &chan->par->pci_dev->dev;
100 chan->algo.setsda = nvidia_gpio_setsda;
101 chan->algo.setscl = nvidia_gpio_setscl;
102 chan->algo.getsda = nvidia_gpio_getsda;
103 chan->algo.getscl = nvidia_gpio_getscl;
104 chan->algo.udelay = 40;
105 chan->algo.timeout = msecs_to_jiffies(2);
106 chan->algo.data = chan;
107
108 i2c_set_adapdata(&chan->adapter, chan);
109
110 /* Raise SCL and SDA */
111 nvidia_gpio_setsda(chan, 1);
112 nvidia_gpio_setscl(chan, 1);
113 udelay(20);
114
115 rc = i2c_bit_add_bus(&chan->adapter);
116 if (rc == 0)
117 dev_dbg(&chan->par->pci_dev->dev,
118 "I2C bus %s registered.\n", name);
119 else {
120 dev_warn(&chan->par->pci_dev->dev,
121 "Failed to register I2C bus %s.\n", name);
122 chan->par = NULL;
123 }
124
125 return rc;
126 }
127
128 void nvidia_create_i2c_busses(struct nvidia_par *par)
129 {
130 par->bus = 3;
131
132 par->chan[0].par = par;
133 par->chan[1].par = par;
134 par->chan[2].par = par;
135
136 par->chan[0].ddc_base = 0x3e;
137 nvidia_setup_i2c_bus(&par->chan[0], "nvidia #0");
138
139 par->chan[1].ddc_base = 0x36;
140 nvidia_setup_i2c_bus(&par->chan[1], "nvidia #1");
141
142 par->chan[2].ddc_base = 0x50;
143 nvidia_setup_i2c_bus(&par->chan[2], "nvidia #2");
144 }
145
146 void nvidia_delete_i2c_busses(struct nvidia_par *par)
147 {
148 if (par->chan[0].par)
149 i2c_del_adapter(&par->chan[0].adapter);
150 par->chan[0].par = NULL;
151
152 if (par->chan[1].par)
153 i2c_del_adapter(&par->chan[1].adapter);
154 par->chan[1].par = NULL;
155
156 if (par->chan[2].par)
157 i2c_del_adapter(&par->chan[2].adapter);
158 par->chan[2].par = NULL;
159
160 }
161
162 static u8 *nvidia_do_probe_i2c_edid(struct nvidia_i2c_chan *chan)
163 {
164 u8 start = 0x0;
165 struct i2c_msg msgs[] = {
166 {
167 .addr = 0x50,
168 .len = 1,
169 .buf = &start,
170 }, {
171 .addr = 0x50,
172 .flags = I2C_M_RD,
173 .len = EDID_LENGTH,
174 },
175 };
176 u8 *buf;
177
178 if (!chan->par)
179 return NULL;
180
181 buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
182 if (!buf) {
183 dev_warn(&chan->par->pci_dev->dev, "Out of memory!\n");
184 return NULL;
185 }
186 msgs[1].buf = buf;
187
188 if (i2c_transfer(&chan->adapter, msgs, 2) == 2)
189 return buf;
190 dev_dbg(&chan->par->pci_dev->dev, "Unable to read EDID block.\n");
191 kfree(buf);
192 return NULL;
193 }
194
195 int nvidia_probe_i2c_connector(struct fb_info *info, int conn, u8 **out_edid)
196 {
197 struct nvidia_par *par = info->par;
198 u8 *edid = NULL;
199 int i;
200
201 for (i = 0; i < 3; i++) {
202 /* Do the real work */
203 edid = nvidia_do_probe_i2c_edid(&par->chan[conn - 1]);
204 if (edid)
205 break;
206 }
207
208 if (!edid && conn == 1) {
209 /* try to get from firmware */
210 const u8 *e = fb_firmware_edid(info->device);
211
212 if (e != NULL)
213 edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL);
214 }
215
216 *out_edid = edid;
217
218 return (edid) ? 0 : 1;
219 }