2 * Copyright 2012 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 "core/option.h"
27 #include "subdev/i2c.h"
28 #include "subdev/vga.h"
31 nv_rdi2cr(struct nouveau_i2c_port *port, u8 addr, u8 reg)
34 struct i2c_msg msgs[] = {
35 { .addr = addr, .flags = 0, .len = 1, .buf = ® },
36 { .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = &val },
39 int ret = i2c_transfer(&port->adapter, msgs, 2);
47 nv_wri2cr(struct nouveau_i2c_port *port, u8 addr, u8 reg, u8 val)
49 struct i2c_msg msgs[] = {
50 { .addr = addr, .flags = 0, .len = 1, .buf = ® },
51 { .addr = addr, .flags = 0, .len = 1, .buf = &val },
54 int ret = i2c_transfer(&port->adapter, msgs, 2);
62 nv_probe_i2c(struct nouveau_i2c_port *port, u8 addr)
65 struct i2c_msg msgs[] = {
80 return i2c_transfer(&port->adapter, msgs, 2) == 2;
83 static struct nouveau_i2c_port *
84 nouveau_i2c_find(struct nouveau_i2c *i2c, u8 index)
86 struct nouveau_bios *bios = nouveau_bios(i2c);
87 struct nouveau_i2c_port *port;
89 if (index == NV_I2C_DEFAULT(0) ||
90 index == NV_I2C_DEFAULT(1)) {
91 u8 ver, hdr, cnt, len;
92 u16 i2c = dcb_i2c_table(bios, &ver, &hdr, &cnt, &len);
93 if (i2c && ver >= 0x30) {
94 u8 auxidx = nv_ro08(bios, i2c + 4);
95 if (index == NV_I2C_DEFAULT(0))
96 index = (auxidx & 0x0f) >> 0;
98 index = (auxidx & 0xf0) >> 4;
104 list_for_each_entry(port, &i2c->ports, head) {
105 if (port->index == index)
109 if (&port->head == &i2c->ports)
112 if (nv_device(i2c)->card_type >= NV_50 && (port->dcb & 0x00000100)) {
113 u32 reg = 0x00e500, val;
114 if (port->type == 6) {
115 reg += port->drive * 0x50;
118 reg += ((port->dcb & 0x1e00) >> 9) * 0x50;
122 /* nfi, but neither auxch or i2c work if it's 1 */
123 nv_mask(i2c, reg + 0x0c, 0x00000001, 0x00000000);
124 /* nfi, but switches auxch vs normal i2c */
125 nv_mask(i2c, reg + 0x00, 0x0000f003, val);
132 nouveau_i2c_identify(struct nouveau_i2c *i2c, int index, const char *what,
133 struct i2c_board_info *info,
134 bool (*match)(struct nouveau_i2c_port *,
135 struct i2c_board_info *))
137 struct nouveau_i2c_port *port = nouveau_i2c_find(i2c, index);
141 nv_debug(i2c, "no bus when probing %s on %d\n", what, index);
145 nv_debug(i2c, "probing %ss on bus: %d\n", what, port->index);
146 for (i = 0; info[i].addr; i++) {
147 if (nv_probe_i2c(port, info[i].addr) &&
148 (!match || match(port, &info[i]))) {
149 nv_info(i2c, "detected %s: %s\n", what, info[i].type);
154 nv_debug(i2c, "no devices found.\n");
159 nouveau_i2c_drive_scl(void *data, int state)
161 struct nouveau_i2c_port *port = data;
163 if (port->type == DCB_I2C_NV04_BIT) {
164 u8 val = nv_rdvgac(port->i2c, 0, port->drive);
165 if (state) val |= 0x20;
167 nv_wrvgac(port->i2c, 0, port->drive, val | 0x01);
169 if (port->type == DCB_I2C_NV4E_BIT) {
170 nv_mask(port->i2c, port->drive, 0x2f, state ? 0x21 : 0x01);
172 if (port->type == DCB_I2C_NVIO_BIT) {
173 if (state) port->state |= 0x01;
174 else port->state &= 0xfe;
175 nv_wr32(port->i2c, port->drive, 4 | port->state);
180 nouveau_i2c_drive_sda(void *data, int state)
182 struct nouveau_i2c_port *port = data;
184 if (port->type == DCB_I2C_NV04_BIT) {
185 u8 val = nv_rdvgac(port->i2c, 0, port->drive);
186 if (state) val |= 0x10;
188 nv_wrvgac(port->i2c, 0, port->drive, val | 0x01);
190 if (port->type == DCB_I2C_NV4E_BIT) {
191 nv_mask(port->i2c, port->drive, 0x1f, state ? 0x11 : 0x01);
193 if (port->type == DCB_I2C_NVIO_BIT) {
194 if (state) port->state |= 0x02;
195 else port->state &= 0xfd;
196 nv_wr32(port->i2c, port->drive, 4 | port->state);
201 nouveau_i2c_sense_scl(void *data)
203 struct nouveau_i2c_port *port = data;
204 struct nouveau_device *device = nv_device(port->i2c);
206 if (port->type == DCB_I2C_NV04_BIT) {
207 return !!(nv_rdvgac(port->i2c, 0, port->sense) & 0x04);
209 if (port->type == DCB_I2C_NV4E_BIT) {
210 return !!(nv_rd32(port->i2c, port->sense) & 0x00040000);
212 if (port->type == DCB_I2C_NVIO_BIT) {
213 if (device->card_type < NV_D0)
214 return !!(nv_rd32(port->i2c, port->sense) & 0x01);
216 return !!(nv_rd32(port->i2c, port->sense) & 0x10);
223 nouveau_i2c_sense_sda(void *data)
225 struct nouveau_i2c_port *port = data;
226 struct nouveau_device *device = nv_device(port->i2c);
228 if (port->type == DCB_I2C_NV04_BIT) {
229 return !!(nv_rdvgac(port->i2c, 0, port->sense) & 0x08);
231 if (port->type == DCB_I2C_NV4E_BIT) {
232 return !!(nv_rd32(port->i2c, port->sense) & 0x00080000);
234 if (port->type == DCB_I2C_NVIO_BIT) {
235 if (device->card_type < NV_D0)
236 return !!(nv_rd32(port->i2c, port->sense) & 0x02);
238 return !!(nv_rd32(port->i2c, port->sense) & 0x20);
244 static const u32 nv50_i2c_port[] = {
245 0x00e138, 0x00e150, 0x00e168, 0x00e180,
246 0x00e254, 0x00e274, 0x00e764, 0x00e780,
251 nouveau_i2c_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
252 struct nouveau_oclass *oclass, void *data, u32 size,
253 struct nouveau_object **pobject)
255 struct nouveau_device *device = nv_device(parent);
256 struct nouveau_bios *bios = nouveau_bios(parent);
257 struct nouveau_i2c_port *port;
258 struct nouveau_i2c *i2c;
259 struct dcb_i2c_entry info;
262 ret = nouveau_subdev_create(parent, engine, oclass, 0,
264 *pobject = nv_object(i2c);
268 i2c->find = nouveau_i2c_find;
269 i2c->identify = nouveau_i2c_identify;
270 INIT_LIST_HEAD(&i2c->ports);
272 while (!dcb_i2c_parse(bios, ++i, &info)) {
273 if (info.type == DCB_I2C_UNUSED)
276 port = kzalloc(sizeof(*port), GFP_KERNEL);
278 nv_error(i2c, "failed port memory alloc at %d\n", i);
282 port->type = info.type;
283 switch (port->type) {
284 case DCB_I2C_NV04_BIT:
285 port->drive = info.drive;
286 port->sense = info.sense;
288 case DCB_I2C_NV4E_BIT:
289 port->drive = 0x600800 + info.drive;
290 port->sense = port->drive;
292 case DCB_I2C_NVIO_BIT:
293 port->drive = info.drive & 0x0f;
294 if (device->card_type < NV_D0) {
295 if (info.drive >= ARRAY_SIZE(nv50_i2c_port))
297 port->drive = nv50_i2c_port[port->drive];
298 port->sense = port->drive;
300 port->drive = 0x00d014 + (port->drive * 0x20);
301 port->sense = port->drive;
304 case DCB_I2C_NVIO_AUX:
305 port->drive = info.drive & 0x0f;
306 port->sense = port->drive;
307 port->adapter.algo = &nouveau_i2c_aux_algo;
313 if (!port->adapter.algo && !port->drive) {
314 nv_error(i2c, "I2C%d: type %d index %x/%x unknown\n",
315 i, port->type, port->drive, port->sense);
320 snprintf(port->adapter.name, sizeof(port->adapter.name),
321 "nouveau-%s-%d", device->name, i);
322 port->adapter.owner = THIS_MODULE;
323 port->adapter.dev.parent = &device->pdev->dev;
326 port->dcb = info.data;
327 i2c_set_adapdata(&port->adapter, i2c);
329 if (port->adapter.algo != &nouveau_i2c_aux_algo) {
330 nouveau_i2c_drive_scl(port, 0);
331 nouveau_i2c_drive_sda(port, 1);
332 nouveau_i2c_drive_scl(port, 1);
334 #ifdef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
335 if (nouveau_boolopt(device->cfgopt, "NvI2C", true)) {
337 if (nouveau_boolopt(device->cfgopt, "NvI2C", false)) {
339 port->adapter.algo = &nouveau_i2c_bit_algo;
340 ret = i2c_add_adapter(&port->adapter);
342 port->adapter.algo_data = &port->bit;
343 port->bit.udelay = 10;
344 port->bit.timeout = usecs_to_jiffies(2200);
345 port->bit.data = port;
346 port->bit.setsda = nouveau_i2c_drive_sda;
347 port->bit.setscl = nouveau_i2c_drive_scl;
348 port->bit.getsda = nouveau_i2c_sense_sda;
349 port->bit.getscl = nouveau_i2c_sense_scl;
350 ret = i2c_bit_add_bus(&port->adapter);
353 port->adapter.algo = &nouveau_i2c_aux_algo;
354 ret = i2c_add_adapter(&port->adapter);
358 nv_error(i2c, "I2C%d: failed register: %d\n", i, ret);
363 list_add_tail(&port->head, &i2c->ports);
370 nouveau_i2c_dtor(struct nouveau_object *object)
372 struct nouveau_i2c *i2c = (void *)object;
373 struct nouveau_i2c_port *port, *temp;
375 list_for_each_entry_safe(port, temp, &i2c->ports, head) {
376 i2c_del_adapter(&port->adapter);
377 list_del(&port->head);
381 nouveau_subdev_destroy(&i2c->base);
385 nouveau_i2c_init(struct nouveau_object *object)
387 struct nouveau_i2c *i2c = (void *)object;
388 return nouveau_subdev_init(&i2c->base);
392 nouveau_i2c_fini(struct nouveau_object *object, bool suspend)
394 struct nouveau_i2c *i2c = (void *)object;
395 return nouveau_subdev_fini(&i2c->base, suspend);
398 struct nouveau_oclass
399 nouveau_i2c_oclass = {
400 .handle = NV_SUBDEV(I2C, 0x00),
401 .ofuncs = &(struct nouveau_ofuncs) {
402 .ctor = nouveau_i2c_ctor,
403 .dtor = nouveau_i2c_dtor,
404 .init = nouveau_i2c_init,
405 .fini = nouveau_i2c_fini,