1/* $NetBSD: nouveau_subdev_i2c_base.c,v 1.1.1.1 2014/08/06 12:36:30 riastradh Exp $ */
2
3/*
4 * Copyright 2013 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_i2c_base.c,v 1.1.1.1 2014/08/06 12:36:30 riastradh Exp $");
29
30#include <core/option.h>
31
32#include <subdev/bios.h>
33#include <subdev/bios/dcb.h>
34#include <subdev/bios/i2c.h>
35#include <subdev/i2c.h>
36#include <subdev/vga.h>
37
38/******************************************************************************
39 * interface to linux i2c bit-banging algorithm
40 *****************************************************************************/
41
42#ifdef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
43#define CSTMSEL true
44#else
45#define CSTMSEL false
46#endif
47
48static int
49nouveau_i2c_pre_xfer(struct i2c_adapter *adap)
50{
51 struct i2c_algo_bit_data *bit = adap->algo_data;
52 struct nouveau_i2c_port *port = bit->data;
53 if (port->func->acquire)
54 port->func->acquire(port);
55 return 0;
56}
57
58static void
59nouveau_i2c_setscl(void *data, int state)
60{
61 struct nouveau_i2c_port *port = data;
62 port->func->drive_scl(port, state);
63}
64
65static void
66nouveau_i2c_setsda(void *data, int state)
67{
68 struct nouveau_i2c_port *port = data;
69 port->func->drive_sda(port, state);
70}
71
72static int
73nouveau_i2c_getscl(void *data)
74{
75 struct nouveau_i2c_port *port = data;
76 return port->func->sense_scl(port);
77}
78
79static int
80nouveau_i2c_getsda(void *data)
81{
82 struct nouveau_i2c_port *port = data;
83 return port->func->sense_sda(port);
84}
85
86/******************************************************************************
87 * base i2c "port" class implementation
88 *****************************************************************************/
89
90void
91_nouveau_i2c_port_dtor(struct nouveau_object *object)
92{
93 struct nouveau_i2c_port *port = (void *)object;
94 i2c_del_adapter(&port->adapter);
95 nouveau_object_destroy(&port->base);
96}
97
98int
99nouveau_i2c_port_create_(struct nouveau_object *parent,
100 struct nouveau_object *engine,
101 struct nouveau_oclass *oclass, u8 index,
102 const struct i2c_algorithm *algo,
103 const struct nouveau_i2c_func *func,
104 int size, void **pobject)
105{
106 struct nouveau_device *device = nv_device(parent);
107 struct nouveau_i2c *i2c = (void *)engine;
108 struct nouveau_i2c_port *port;
109 int ret;
110
111 ret = nouveau_object_create_(parent, engine, oclass, 0, size, pobject);
112 port = *pobject;
113 if (ret)
114 return ret;
115
116 snprintf(port->adapter.name, sizeof(port->adapter.name),
117 "nouveau-%s-%d", device->name, index);
118 port->adapter.owner = THIS_MODULE;
119 port->adapter.dev.parent = nv_device_base(device);
120 port->index = index;
121 port->func = func;
122 i2c_set_adapdata(&port->adapter, i2c);
123
124 if ( algo == &nouveau_i2c_bit_algo &&
125 !nouveau_boolopt(device->cfgopt, "NvI2C", CSTMSEL)) {
126 struct i2c_algo_bit_data *bit;
127
128 bit = kzalloc(sizeof(*bit), GFP_KERNEL);
129 if (!bit)
130 return -ENOMEM;
131
132 bit->udelay = 10;
133 bit->timeout = usecs_to_jiffies(2200);
134 bit->data = port;
135 bit->pre_xfer = nouveau_i2c_pre_xfer;
136 bit->setsda = nouveau_i2c_setsda;
137 bit->setscl = nouveau_i2c_setscl;
138 bit->getsda = nouveau_i2c_getsda;
139 bit->getscl = nouveau_i2c_getscl;
140
141 port->adapter.algo_data = bit;
142 ret = i2c_bit_add_bus(&port->adapter);
143 } else {
144 port->adapter.algo_data = port;
145 port->adapter.algo = algo;
146 ret = i2c_add_adapter(&port->adapter);
147 }
148
149 /* drop port's i2c subdev refcount, i2c handles this itself */
150 if (ret == 0)
151 list_add_tail(&port->head, &i2c->ports);
152 return ret;
153}
154
155/******************************************************************************
156 * base i2c subdev class implementation
157 *****************************************************************************/
158
159static struct nouveau_i2c_port *
160nouveau_i2c_find(struct nouveau_i2c *i2c, u8 index)
161{
162 struct nouveau_bios *bios = nouveau_bios(i2c);
163 struct nouveau_i2c_port *port;
164
165 if (index == NV_I2C_DEFAULT(0) ||
166 index == NV_I2C_DEFAULT(1)) {
167 u8 ver, hdr, cnt, len;
168 u16 i2c = dcb_i2c_table(bios, &ver, &hdr, &cnt, &len);
169 if (i2c && ver >= 0x30) {
170 u8 auxidx = nv_ro08(bios, i2c + 4);
171 if (index == NV_I2C_DEFAULT(0))
172 index = (auxidx & 0x0f) >> 0;
173 else
174 index = (auxidx & 0xf0) >> 4;
175 } else {
176 index = 2;
177 }
178 }
179
180 list_for_each_entry(port, &i2c->ports, head) {
181 if (port->index == index)
182 return port;
183 }
184
185 return NULL;
186}
187
188static struct nouveau_i2c_port *
189nouveau_i2c_find_type(struct nouveau_i2c *i2c, u16 type)
190{
191 struct nouveau_i2c_port *port;
192
193 list_for_each_entry(port, &i2c->ports, head) {
194 if (nv_hclass(port) == type)
195 return port;
196 }
197
198 return NULL;
199}
200
201static int
202nouveau_i2c_identify(struct nouveau_i2c *i2c, int index, const char *what,
203 struct nouveau_i2c_board_info *info,
204 bool (*match)(struct nouveau_i2c_port *,
205 struct i2c_board_info *, void *), void *data)
206{
207 struct nouveau_i2c_port *port = nouveau_i2c_find(i2c, index);
208 int i;
209
210 if (!port) {
211 nv_debug(i2c, "no bus when probing %s on %d\n", what, index);
212 return -ENODEV;
213 }
214
215 nv_debug(i2c, "probing %ss on bus: %d\n", what, port->index);
216 for (i = 0; info[i].dev.addr; i++) {
217 u8 orig_udelay = 0;
218
219 if ((port->adapter.algo == &i2c_bit_algo) &&
220 (info[i].udelay != 0)) {
221 struct i2c_algo_bit_data *algo = port->adapter.algo_data;
222 nv_debug(i2c, "using custom udelay %d instead of %d\n",
223 info[i].udelay, algo->udelay);
224 orig_udelay = algo->udelay;
225 algo->udelay = info[i].udelay;
226 }
227
228 if (nv_probe_i2c(port, info[i].dev.addr) &&
229 (!match || match(port, &info[i].dev, data))) {
230 nv_info(i2c, "detected %s: %s\n", what,
231 info[i].dev.type);
232 return i;
233 }
234
235 if (orig_udelay) {
236 struct i2c_algo_bit_data *algo = port->adapter.algo_data;
237 algo->udelay = orig_udelay;
238 }
239 }
240
241 nv_debug(i2c, "no devices found.\n");
242 return -ENODEV;
243}
244
245int
246_nouveau_i2c_fini(struct nouveau_object *object, bool suspend)
247{
248 struct nouveau_i2c *i2c = (void *)object;
249 struct nouveau_i2c_port *port;
250 int ret;
251
252 list_for_each_entry(port, &i2c->ports, head) {
253 ret = nv_ofuncs(port)->fini(nv_object(port), suspend);
254 if (ret && suspend)
255 goto fail;
256 }
257
258 return nouveau_subdev_fini(&i2c->base, suspend);
259fail:
260 list_for_each_entry_continue_reverse(port, &i2c->ports, head) {
261 nv_ofuncs(port)->init(nv_object(port));
262 }
263
264 return ret;
265}
266
267int
268_nouveau_i2c_init(struct nouveau_object *object)
269{
270 struct nouveau_i2c *i2c = (void *)object;
271 struct nouveau_i2c_port *port;
272 int ret;
273
274 ret = nouveau_subdev_init(&i2c->base);
275 if (ret == 0) {
276 list_for_each_entry(port, &i2c->ports, head) {
277 ret = nv_ofuncs(port)->init(nv_object(port));
278 if (ret)
279 goto fail;
280 }
281 }
282
283 return ret;
284fail:
285 list_for_each_entry_continue_reverse(port, &i2c->ports, head) {
286 nv_ofuncs(port)->fini(nv_object(port), false);
287 }
288
289 return ret;
290}
291
292void
293_nouveau_i2c_dtor(struct nouveau_object *object)
294{
295 struct nouveau_i2c *i2c = (void *)object;
296 struct nouveau_i2c_port *port, *temp;
297
298 list_for_each_entry_safe(port, temp, &i2c->ports, head) {
299 nouveau_object_ref(NULL, (struct nouveau_object **)&port);
300 }
301
302 nouveau_subdev_destroy(&i2c->base);
303}
304
305static struct nouveau_oclass *
306nouveau_i2c_extdev_sclass[] = {
307 nouveau_anx9805_sclass,
308};
309
310int
311nouveau_i2c_create_(struct nouveau_object *parent,
312 struct nouveau_object *engine,
313 struct nouveau_oclass *oclass,
314 struct nouveau_oclass *sclass,
315 int length, void **pobject)
316{
317 struct nouveau_bios *bios = nouveau_bios(parent);
318 struct nouveau_i2c *i2c;
319 struct nouveau_object *object;
320 struct dcb_i2c_entry info;
321 int ret, i, j, index = -1;
322 struct dcb_output outp;
323 u8 ver, hdr;
324 u32 data;
325
326 ret = nouveau_subdev_create(parent, engine, oclass, 0,
327 "I2C", "i2c", &i2c);
328 *pobject = nv_object(i2c);
329 if (ret)
330 return ret;
331
332 i2c->find = nouveau_i2c_find;
333 i2c->find_type = nouveau_i2c_find_type;
334 i2c->identify = nouveau_i2c_identify;
335 INIT_LIST_HEAD(&i2c->ports);
336
337 while (!dcb_i2c_parse(bios, ++index, &info)) {
338 if (info.type == DCB_I2C_UNUSED)
339 continue;
340
341 oclass = sclass;
342 do {
343 ret = -EINVAL;
344 if (oclass->handle == info.type) {
345 ret = nouveau_object_ctor(*pobject, *pobject,
346 oclass, &info,
347 index, &object);
348 }
349 } while (ret && (++oclass)->handle);
350 }
351
352 /* in addition to the busses specified in the i2c table, there
353 * may be ddc/aux channels hiding behind external tmds/dp/etc
354 * transmitters.
355 */
356 index = ((index + 0x0f) / 0x10) * 0x10;
357 i = -1;
358 while ((data = dcb_outp_parse(bios, ++i, &ver, &hdr, &outp))) {
359 if (!outp.location || !outp.extdev)
360 continue;
361
362 switch (outp.type) {
363 case DCB_OUTPUT_TMDS:
364 info.type = NV_I2C_TYPE_EXTDDC(outp.extdev);
365 break;
366 case DCB_OUTPUT_DP:
367 info.type = NV_I2C_TYPE_EXTAUX(outp.extdev);
368 break;
369 default:
370 continue;
371 }
372
373 ret = -ENODEV;
374 j = -1;
375 while (ret && ++j < ARRAY_SIZE(nouveau_i2c_extdev_sclass)) {
376 parent = nv_object(i2c->find(i2c, outp.i2c_index));
377 oclass = nouveau_i2c_extdev_sclass[j];
378 do {
379 if (oclass->handle != info.type)
380 continue;
381 ret = nouveau_object_ctor(parent, *pobject,
382 oclass, NULL,
383 index++, &object);
384 } while (ret && (++oclass)->handle);
385 }
386 }
387
388 return 0;
389}
390