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.
26 #include "nouveau_drv.h"
27 #include "nouveau_i2c.h"
28 #include "nouveau_hw.h"
31 nv04_i2c_setscl(void *data, int state)
33 struct nouveau_i2c_chan *i2c = data;
34 struct drm_device *dev = i2c->dev;
37 val = (NVReadVgaCrtc(dev, 0, i2c->wr) & 0xd0) | (state ? 0x20 : 0);
38 NVWriteVgaCrtc(dev, 0, i2c->wr, val | 0x01);
42 nv04_i2c_setsda(void *data, int state)
44 struct nouveau_i2c_chan *i2c = data;
45 struct drm_device *dev = i2c->dev;
48 val = (NVReadVgaCrtc(dev, 0, i2c->wr) & 0xe0) | (state ? 0x10 : 0);
49 NVWriteVgaCrtc(dev, 0, i2c->wr, val | 0x01);
53 nv04_i2c_getscl(void *data)
55 struct nouveau_i2c_chan *i2c = data;
56 struct drm_device *dev = i2c->dev;
58 return !!(NVReadVgaCrtc(dev, 0, i2c->rd) & 4);
62 nv04_i2c_getsda(void *data)
64 struct nouveau_i2c_chan *i2c = data;
65 struct drm_device *dev = i2c->dev;
67 return !!(NVReadVgaCrtc(dev, 0, i2c->rd) & 8);
71 nv4e_i2c_setscl(void *data, int state)
73 struct nouveau_i2c_chan *i2c = data;
74 struct drm_device *dev = i2c->dev;
77 val = (nv_rd32(dev, i2c->wr) & 0xd0) | (state ? 0x20 : 0);
78 nv_wr32(dev, i2c->wr, val | 0x01);
82 nv4e_i2c_setsda(void *data, int state)
84 struct nouveau_i2c_chan *i2c = data;
85 struct drm_device *dev = i2c->dev;
88 val = (nv_rd32(dev, i2c->wr) & 0xe0) | (state ? 0x10 : 0);
89 nv_wr32(dev, i2c->wr, val | 0x01);
93 nv4e_i2c_getscl(void *data)
95 struct nouveau_i2c_chan *i2c = data;
96 struct drm_device *dev = i2c->dev;
98 return !!((nv_rd32(dev, i2c->rd) >> 16) & 4);
102 nv4e_i2c_getsda(void *data)
104 struct nouveau_i2c_chan *i2c = data;
105 struct drm_device *dev = i2c->dev;
107 return !!((nv_rd32(dev, i2c->rd) >> 16) & 8);
111 nv50_i2c_getscl(void *data)
113 struct nouveau_i2c_chan *i2c = data;
114 struct drm_device *dev = i2c->dev;
116 return !!(nv_rd32(dev, i2c->rd) & 1);
121 nv50_i2c_getsda(void *data)
123 struct nouveau_i2c_chan *i2c = data;
124 struct drm_device *dev = i2c->dev;
126 return !!(nv_rd32(dev, i2c->rd) & 2);
130 nv50_i2c_setscl(void *data, int state)
132 struct nouveau_i2c_chan *i2c = data;
133 struct drm_device *dev = i2c->dev;
135 nv_wr32(dev, i2c->wr, 4 | (i2c->data ? 2 : 0) | (state ? 1 : 0));
139 nv50_i2c_setsda(void *data, int state)
141 struct nouveau_i2c_chan *i2c = data;
142 struct drm_device *dev = i2c->dev;
144 nv_wr32(dev, i2c->wr,
145 (nv_rd32(dev, i2c->rd) & 1) | 4 | (state ? 2 : 0));
149 static const uint32_t nv50_i2c_port[] = {
150 0x00e138, 0x00e150, 0x00e168, 0x00e180,
151 0x00e254, 0x00e274, 0x00e764, 0x00e780,
154 #define NV50_I2C_PORTS ARRAY_SIZE(nv50_i2c_port)
157 nouveau_i2c_init(struct drm_device *dev, struct dcb_i2c_entry *entry, int index)
159 struct drm_nouveau_private *dev_priv = dev->dev_private;
160 struct nouveau_i2c_chan *i2c;
166 if (dev_priv->card_type == NV_50 && entry->read >= NV50_I2C_PORTS) {
167 NV_ERROR(dev, "unknown i2c port %d\n", entry->read);
171 i2c = kzalloc(sizeof(*i2c), GFP_KERNEL);
175 switch (entry->port_type) {
177 i2c->algo.bit.setsda = nv04_i2c_setsda;
178 i2c->algo.bit.setscl = nv04_i2c_setscl;
179 i2c->algo.bit.getsda = nv04_i2c_getsda;
180 i2c->algo.bit.getscl = nv04_i2c_getscl;
181 i2c->rd = entry->read;
182 i2c->wr = entry->write;
185 i2c->algo.bit.setsda = nv4e_i2c_setsda;
186 i2c->algo.bit.setscl = nv4e_i2c_setscl;
187 i2c->algo.bit.getsda = nv4e_i2c_getsda;
188 i2c->algo.bit.getscl = nv4e_i2c_getscl;
189 i2c->rd = 0x600800 + entry->read;
190 i2c->wr = 0x600800 + entry->write;
193 i2c->algo.bit.setsda = nv50_i2c_setsda;
194 i2c->algo.bit.setscl = nv50_i2c_setscl;
195 i2c->algo.bit.getsda = nv50_i2c_getsda;
196 i2c->algo.bit.getscl = nv50_i2c_getscl;
197 i2c->rd = nv50_i2c_port[entry->read];
201 i2c->rd = entry->read;
202 i2c->wr = entry->write;
205 NV_ERROR(dev, "DCB I2C port type %d unknown\n",
211 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
212 "nouveau-%s-%d", pci_name(dev->pdev), index);
213 i2c->adapter.owner = THIS_MODULE;
214 i2c->adapter.dev.parent = &dev->pdev->dev;
216 i2c_set_adapdata(&i2c->adapter, i2c);
218 if (entry->port_type < 6) {
219 i2c->adapter.algo_data = &i2c->algo.bit;
220 i2c->algo.bit.udelay = 40;
221 i2c->algo.bit.timeout = usecs_to_jiffies(5000);
222 i2c->algo.bit.data = i2c;
223 ret = i2c_bit_add_bus(&i2c->adapter);
225 i2c->adapter.algo_data = &i2c->algo.dp;
226 i2c->algo.dp.running = false;
227 i2c->algo.dp.address = 0;
228 i2c->algo.dp.aux_ch = nouveau_dp_i2c_aux_ch;
229 ret = i2c_dp_aux_add_bus(&i2c->adapter);
233 NV_ERROR(dev, "Failed to register i2c %d\n", index);
243 nouveau_i2c_fini(struct drm_device *dev, struct dcb_i2c_entry *entry)
248 i2c_del_adapter(&entry->chan->adapter);
253 struct nouveau_i2c_chan *
254 nouveau_i2c_find(struct drm_device *dev, int index)
256 struct drm_nouveau_private *dev_priv = dev->dev_private;
257 struct nvbios *bios = &dev_priv->VBIOS;
259 if (index > DCB_MAX_NUM_I2C_ENTRIES)
262 if (!bios->bdcb.dcb.i2c[index].chan) {
263 if (nouveau_i2c_init(dev, &bios->bdcb.dcb.i2c[index], index))
267 return bios->bdcb.dcb.i2c[index].chan;