2 * Copyright 2009 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
25 #include <linux/module.h>
28 #include "nouveau_drv.h"
29 #include "nouveau_i2c.h"
30 #include "nouveau_hw.h"
33 nv04_i2c_setscl(void *data, int state)
35 struct nouveau_i2c_chan *i2c = data;
36 struct drm_device *dev = i2c->dev;
39 val = (NVReadVgaCrtc(dev, 0, i2c->wr) & 0xd0) | (state ? 0x20 : 0);
40 NVWriteVgaCrtc(dev, 0, i2c->wr, val | 0x01);
44 nv04_i2c_setsda(void *data, int state)
46 struct nouveau_i2c_chan *i2c = data;
47 struct drm_device *dev = i2c->dev;
50 val = (NVReadVgaCrtc(dev, 0, i2c->wr) & 0xe0) | (state ? 0x10 : 0);
51 NVWriteVgaCrtc(dev, 0, i2c->wr, val | 0x01);
55 nv04_i2c_getscl(void *data)
57 struct nouveau_i2c_chan *i2c = data;
58 struct drm_device *dev = i2c->dev;
60 return !!(NVReadVgaCrtc(dev, 0, i2c->rd) & 4);
64 nv04_i2c_getsda(void *data)
66 struct nouveau_i2c_chan *i2c = data;
67 struct drm_device *dev = i2c->dev;
69 return !!(NVReadVgaCrtc(dev, 0, i2c->rd) & 8);
73 nv4e_i2c_setscl(void *data, int state)
75 struct nouveau_i2c_chan *i2c = data;
76 struct drm_device *dev = i2c->dev;
79 val = (nv_rd32(dev, i2c->wr) & 0xd0) | (state ? 0x20 : 0);
80 nv_wr32(dev, i2c->wr, val | 0x01);
84 nv4e_i2c_setsda(void *data, int state)
86 struct nouveau_i2c_chan *i2c = data;
87 struct drm_device *dev = i2c->dev;
90 val = (nv_rd32(dev, i2c->wr) & 0xe0) | (state ? 0x10 : 0);
91 nv_wr32(dev, i2c->wr, val | 0x01);
95 nv4e_i2c_getscl(void *data)
97 struct nouveau_i2c_chan *i2c = data;
98 struct drm_device *dev = i2c->dev;
100 return !!((nv_rd32(dev, i2c->rd) >> 16) & 4);
104 nv4e_i2c_getsda(void *data)
106 struct nouveau_i2c_chan *i2c = data;
107 struct drm_device *dev = i2c->dev;
109 return !!((nv_rd32(dev, i2c->rd) >> 16) & 8);
112 static const uint32_t nv50_i2c_port[] = {
113 0x00e138, 0x00e150, 0x00e168, 0x00e180,
114 0x00e254, 0x00e274, 0x00e764, 0x00e780,
117 #define NV50_I2C_PORTS ARRAY_SIZE(nv50_i2c_port)
120 nv50_i2c_getscl(void *data)
122 struct nouveau_i2c_chan *i2c = data;
123 struct drm_device *dev = i2c->dev;
125 return !!(nv_rd32(dev, i2c->rd) & 1);
130 nv50_i2c_getsda(void *data)
132 struct nouveau_i2c_chan *i2c = data;
133 struct drm_device *dev = i2c->dev;
135 return !!(nv_rd32(dev, i2c->rd) & 2);
139 nv50_i2c_setscl(void *data, int state)
141 struct nouveau_i2c_chan *i2c = data;
143 nv_wr32(i2c->dev, i2c->wr, 4 | (i2c->data ? 2 : 0) | (state ? 1 : 0));
147 nv50_i2c_setsda(void *data, int state)
149 struct nouveau_i2c_chan *i2c = data;
151 nv_mask(i2c->dev, i2c->wr, 0x00000006, 4 | (state ? 2 : 0));
156 nvd0_i2c_getscl(void *data)
158 struct nouveau_i2c_chan *i2c = data;
159 return !!(nv_rd32(i2c->dev, i2c->rd) & 0x10);
163 nvd0_i2c_getsda(void *data)
165 struct nouveau_i2c_chan *i2c = data;
166 return !!(nv_rd32(i2c->dev, i2c->rd) & 0x20);
170 nouveau_i2c_init(struct drm_device *dev, struct dcb_i2c_entry *entry, int index)
172 struct drm_nouveau_private *dev_priv = dev->dev_private;
173 struct nouveau_i2c_chan *i2c;
179 if (dev_priv->card_type >= NV_50 &&
180 dev_priv->card_type <= NV_C0 && entry->read >= NV50_I2C_PORTS) {
181 NV_ERROR(dev, "unknown i2c port %d\n", entry->read);
185 i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
189 switch (entry->port_type) {
191 i2c->bit.setsda = nv04_i2c_setsda;
192 i2c->bit.setscl = nv04_i2c_setscl;
193 i2c->bit.getsda = nv04_i2c_getsda;
194 i2c->bit.getscl = nv04_i2c_getscl;
195 i2c->rd = entry->read;
196 i2c->wr = entry->write;
199 i2c->bit.setsda = nv4e_i2c_setsda;
200 i2c->bit.setscl = nv4e_i2c_setscl;
201 i2c->bit.getsda = nv4e_i2c_getsda;
202 i2c->bit.getscl = nv4e_i2c_getscl;
203 i2c->rd = 0x600800 + entry->read;
204 i2c->wr = 0x600800 + entry->write;
207 i2c->bit.setsda = nv50_i2c_setsda;
208 i2c->bit.setscl = nv50_i2c_setscl;
209 if (dev_priv->card_type < NV_D0) {
210 i2c->bit.getsda = nv50_i2c_getsda;
211 i2c->bit.getscl = nv50_i2c_getscl;
212 i2c->rd = nv50_i2c_port[entry->read];
215 i2c->bit.getsda = nvd0_i2c_getsda;
216 i2c->bit.getscl = nvd0_i2c_getscl;
217 i2c->rd = 0x00d014 + (entry->read * 0x20);
222 i2c->rd = entry->read;
223 i2c->wr = entry->write;
226 NV_ERROR(dev, "DCB I2C port type %d unknown\n",
232 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
233 "nouveau-%s-%d", pci_name(dev->pdev), index);
234 i2c->adapter.owner = THIS_MODULE;
235 i2c->adapter.dev.parent = &dev->pdev->dev;
237 i2c_set_adapdata(&i2c->adapter, i2c);
239 if (entry->port_type < 6) {
240 i2c->adapter.algo_data = &i2c->bit;
241 i2c->bit.udelay = 40;
242 i2c->bit.timeout = usecs_to_jiffies(5000);
244 ret = i2c_bit_add_bus(&i2c->adapter);
246 i2c->adapter.algo = &nouveau_dp_i2c_algo;
247 ret = i2c_add_adapter(&i2c->adapter);
251 NV_ERROR(dev, "Failed to register i2c %d\n", index);
261 nouveau_i2c_fini(struct drm_device *dev, struct dcb_i2c_entry *entry)
266 i2c_del_adapter(&entry->chan->adapter);
271 struct nouveau_i2c_chan *
272 nouveau_i2c_find(struct drm_device *dev, int index)
274 struct drm_nouveau_private *dev_priv = dev->dev_private;
275 struct dcb_i2c_entry *i2c = &dev_priv->vbios.dcb.i2c[index];
277 if (index >= DCB_MAX_NUM_I2C_ENTRIES)
280 if (dev_priv->card_type >= NV_50 && (i2c->entry & 0x00000100)) {
281 uint32_t reg = 0xe500, val;
283 if (i2c->port_type == 6) {
284 reg += i2c->read * 0x50;
287 reg += ((i2c->entry & 0x1e00) >> 9) * 0x50;
291 /* nfi, but neither auxch or i2c work if it's 1 */
292 nv_mask(dev, reg + 0x0c, 0x00000001, 0x00000000);
293 /* nfi, but switches auxch vs normal i2c */
294 nv_mask(dev, reg + 0x00, 0x0000f003, val);
297 if (!i2c->chan && nouveau_i2c_init(dev, i2c, index))
303 nouveau_probe_i2c_addr(struct nouveau_i2c_chan *i2c, int addr)
305 uint8_t buf[] = { 0 };
306 struct i2c_msg msgs[] = {
321 return i2c_transfer(&i2c->adapter, msgs, 2) == 2;
325 nouveau_i2c_identify(struct drm_device *dev, const char *what,
326 struct i2c_board_info *info,
327 bool (*match)(struct nouveau_i2c_chan *,
328 struct i2c_board_info *),
331 struct nouveau_i2c_chan *i2c = nouveau_i2c_find(dev, index);
334 NV_DEBUG(dev, "Probing %ss on I2C bus: %d\n", what, index);
336 for (i = 0; i2c && info[i].addr; i++) {
337 if (nouveau_probe_i2c_addr(i2c, info[i].addr) &&
338 (!match || match(i2c, &info[i]))) {
339 NV_INFO(dev, "Detected %s: %s\n", what, info[i].type);
344 NV_DEBUG(dev, "No devices found.\n");