Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
base.c
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors: Ben Skeggs
23  */
24 
25 #include "core/option.h"
26 
27 #include "subdev/i2c.h"
28 #include "subdev/vga.h"
29 
30 int
32 {
33  u8 val;
34  struct i2c_msg msgs[] = {
35  { .addr = addr, .flags = 0, .len = 1, .buf = &reg },
36  { .addr = addr, .flags = I2C_M_RD, .len = 1, .buf = &val },
37  };
38 
39  int ret = i2c_transfer(&port->adapter, msgs, 2);
40  if (ret != 2)
41  return -EIO;
42 
43  return val;
44 }
45 
46 int
48 {
49  struct i2c_msg msgs[] = {
50  { .addr = addr, .flags = 0, .len = 1, .buf = &reg },
51  { .addr = addr, .flags = 0, .len = 1, .buf = &val },
52  };
53 
54  int ret = i2c_transfer(&port->adapter, msgs, 2);
55  if (ret != 2)
56  return -EIO;
57 
58  return 0;
59 }
60 
61 bool
63 {
64  u8 buf[] = { 0 };
65  struct i2c_msg msgs[] = {
66  {
67  .addr = addr,
68  .flags = 0,
69  .len = 1,
70  .buf = buf,
71  },
72  {
73  .addr = addr,
74  .flags = I2C_M_RD,
75  .len = 1,
76  .buf = buf,
77  }
78  };
79 
80  return i2c_transfer(&port->adapter, msgs, 2) == 2;
81 }
82 
83 static struct nouveau_i2c_port *
84 nouveau_i2c_find(struct nouveau_i2c *i2c, u8 index)
85 {
86  struct nouveau_bios *bios = nouveau_bios(i2c);
87  struct nouveau_i2c_port *port;
88 
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;
97  else
98  index = (auxidx & 0xf0) >> 4;
99  } else {
100  index = 2;
101  }
102  }
103 
104  list_for_each_entry(port, &i2c->ports, head) {
105  if (port->index == index)
106  break;
107  }
108 
109  if (&port->head == &i2c->ports)
110  return NULL;
111 
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;
116  val = 0x2002;
117  } else {
118  reg += ((port->dcb & 0x1e00) >> 9) * 0x50;
119  val = 0xe001;
120  }
121 
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);
126  }
127 
128  return port;
129 }
130 
131 static int
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 *))
136 {
137  struct nouveau_i2c_port *port = nouveau_i2c_find(i2c, index);
138  int i;
139 
140  if (!port) {
141  nv_debug(i2c, "no bus when probing %s on %d\n", what, index);
142  return -ENODEV;
143  }
144 
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);
150  return i;
151  }
152  }
153 
154  nv_debug(i2c, "no devices found.\n");
155  return -ENODEV;
156 }
157 
158 void
160 {
161  struct nouveau_i2c_port *port = data;
162 
163  if (port->type == DCB_I2C_NV04_BIT) {
164  u8 val = nv_rdvgac(port->i2c, 0, port->drive);
165  if (state) val |= 0x20;
166  else val &= 0xdf;
167  nv_wrvgac(port->i2c, 0, port->drive, val | 0x01);
168  } else
169  if (port->type == DCB_I2C_NV4E_BIT) {
170  nv_mask(port->i2c, port->drive, 0x2f, state ? 0x21 : 0x01);
171  } else
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);
176  }
177 }
178 
179 void
181 {
182  struct nouveau_i2c_port *port = data;
183 
184  if (port->type == DCB_I2C_NV04_BIT) {
185  u8 val = nv_rdvgac(port->i2c, 0, port->drive);
186  if (state) val |= 0x10;
187  else val &= 0xef;
188  nv_wrvgac(port->i2c, 0, port->drive, val | 0x01);
189  } else
190  if (port->type == DCB_I2C_NV4E_BIT) {
191  nv_mask(port->i2c, port->drive, 0x1f, state ? 0x11 : 0x01);
192  } else
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);
197  }
198 }
199 
200 int
202 {
203  struct nouveau_i2c_port *port = data;
204  struct nouveau_device *device = nv_device(port->i2c);
205 
206  if (port->type == DCB_I2C_NV04_BIT) {
207  return !!(nv_rdvgac(port->i2c, 0, port->sense) & 0x04);
208  } else
209  if (port->type == DCB_I2C_NV4E_BIT) {
210  return !!(nv_rd32(port->i2c, port->sense) & 0x00040000);
211  } else
212  if (port->type == DCB_I2C_NVIO_BIT) {
213  if (device->card_type < NV_D0)
214  return !!(nv_rd32(port->i2c, port->sense) & 0x01);
215  else
216  return !!(nv_rd32(port->i2c, port->sense) & 0x10);
217  }
218 
219  return 0;
220 }
221 
222 int
224 {
225  struct nouveau_i2c_port *port = data;
226  struct nouveau_device *device = nv_device(port->i2c);
227 
228  if (port->type == DCB_I2C_NV04_BIT) {
229  return !!(nv_rdvgac(port->i2c, 0, port->sense) & 0x08);
230  } else
231  if (port->type == DCB_I2C_NV4E_BIT) {
232  return !!(nv_rd32(port->i2c, port->sense) & 0x00080000);
233  } else
234  if (port->type == DCB_I2C_NVIO_BIT) {
235  if (device->card_type < NV_D0)
236  return !!(nv_rd32(port->i2c, port->sense) & 0x02);
237  else
238  return !!(nv_rd32(port->i2c, port->sense) & 0x20);
239  }
240 
241  return 0;
242 }
243 
244 static const u32 nv50_i2c_port[] = {
245  0x00e138, 0x00e150, 0x00e168, 0x00e180,
246  0x00e254, 0x00e274, 0x00e764, 0x00e780,
247  0x00e79c, 0x00e7b8
248 };
249 
250 static int
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)
254 {
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;
260  int ret, i = -1;
261 
262  ret = nouveau_subdev_create(parent, engine, oclass, 0,
263  "I2C", "i2c", &i2c);
264  *pobject = nv_object(i2c);
265  if (ret)
266  return ret;
267 
268  i2c->find = nouveau_i2c_find;
269  i2c->identify = nouveau_i2c_identify;
270  INIT_LIST_HEAD(&i2c->ports);
271 
272  while (!dcb_i2c_parse(bios, ++i, &info)) {
273  if (info.type == DCB_I2C_UNUSED)
274  continue;
275 
276  port = kzalloc(sizeof(*port), GFP_KERNEL);
277  if (!port) {
278  nv_error(i2c, "failed port memory alloc at %d\n", i);
279  break;
280  }
281 
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;
287  break;
288  case DCB_I2C_NV4E_BIT:
289  port->drive = 0x600800 + info.drive;
290  port->sense = port->drive;
291  break;
292  case DCB_I2C_NVIO_BIT:
293  port->drive = info.drive & 0x0f;
294  if (device->card_type < NV_D0) {
295  if (port->drive >= ARRAY_SIZE(nv50_i2c_port))
296  break;
297  port->drive = nv50_i2c_port[port->drive];
298  port->sense = port->drive;
299  } else {
300  port->drive = 0x00d014 + (port->drive * 0x20);
301  port->sense = port->drive;
302  }
303  break;
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;
308  break;
309  default:
310  break;
311  }
312 
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);
316  kfree(port);
317  continue;
318  }
319 
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;
324  port->i2c = i2c;
325  port->index = i;
326  port->dcb = info.data;
327  i2c_set_adapdata(&port->adapter, i2c);
328 
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);
333 
334 #ifdef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
335  if (nouveau_boolopt(device->cfgopt, "NvI2C", true)) {
336 #else
337  if (nouveau_boolopt(device->cfgopt, "NvI2C", false)) {
338 #endif
339  port->adapter.algo = &nouveau_i2c_bit_algo;
340  ret = i2c_add_adapter(&port->adapter);
341  } else {
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);
351  }
352  } else {
353  port->adapter.algo = &nouveau_i2c_aux_algo;
354  ret = i2c_add_adapter(&port->adapter);
355  }
356 
357  if (ret) {
358  nv_error(i2c, "I2C%d: failed register: %d\n", i, ret);
359  kfree(port);
360  continue;
361  }
362 
363  list_add_tail(&port->head, &i2c->ports);
364  }
365 
366  return 0;
367 }
368 
369 static void
370 nouveau_i2c_dtor(struct nouveau_object *object)
371 {
372  struct nouveau_i2c *i2c = (void *)object;
373  struct nouveau_i2c_port *port, *temp;
374 
375  list_for_each_entry_safe(port, temp, &i2c->ports, head) {
376  i2c_del_adapter(&port->adapter);
377  list_del(&port->head);
378  kfree(port);
379  }
380 
382 }
383 
384 static int
385 nouveau_i2c_init(struct nouveau_object *object)
386 {
387  struct nouveau_i2c *i2c = (void *)object;
388  return nouveau_subdev_init(&i2c->base);
389 }
390 
391 static int
392 nouveau_i2c_fini(struct nouveau_object *object, bool suspend)
393 {
394  struct nouveau_i2c *i2c = (void *)object;
395  return nouveau_subdev_fini(&i2c->base, suspend);
396 }
397 
398 struct nouveau_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,
406  },
407 };