Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / gpu / drm / nouveau / nvkm / subdev / i2c / base.c
1 /*
2  * Copyright 2013 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 #include "priv.h"
25 #include "pad.h"
26
27 #include <core/device.h>
28 #include <core/notify.h>
29 #include <core/option.h>
30 #include <subdev/bios.h>
31 #include <subdev/bios/dcb.h>
32
33 /******************************************************************************
34  * interface to linux i2c bit-banging algorithm
35  *****************************************************************************/
36
37 #ifdef CONFIG_NOUVEAU_I2C_INTERNAL_DEFAULT
38 #define CSTMSEL true
39 #else
40 #define CSTMSEL false
41 #endif
42
43 static int
44 nvkm_i2c_pre_xfer(struct i2c_adapter *adap)
45 {
46         struct i2c_algo_bit_data *bit = adap->algo_data;
47         struct nvkm_i2c_port *port = bit->data;
48         return nvkm_i2c(port)->acquire(port, bit->timeout);
49 }
50
51 static void
52 nvkm_i2c_post_xfer(struct i2c_adapter *adap)
53 {
54         struct i2c_algo_bit_data *bit = adap->algo_data;
55         struct nvkm_i2c_port *port = bit->data;
56         return nvkm_i2c(port)->release(port);
57 }
58
59 static void
60 nvkm_i2c_setscl(void *data, int state)
61 {
62         struct nvkm_i2c_port *port = data;
63         port->func->drive_scl(port, state);
64 }
65
66 static void
67 nvkm_i2c_setsda(void *data, int state)
68 {
69         struct nvkm_i2c_port *port = data;
70         port->func->drive_sda(port, state);
71 }
72
73 static int
74 nvkm_i2c_getscl(void *data)
75 {
76         struct nvkm_i2c_port *port = data;
77         return port->func->sense_scl(port);
78 }
79
80 static int
81 nvkm_i2c_getsda(void *data)
82 {
83         struct nvkm_i2c_port *port = data;
84         return port->func->sense_sda(port);
85 }
86
87 /******************************************************************************
88  * base i2c "port" class implementation
89  *****************************************************************************/
90
91 int
92 _nvkm_i2c_port_fini(struct nvkm_object *object, bool suspend)
93 {
94         struct nvkm_i2c_port *port = (void *)object;
95         struct nvkm_i2c_pad *pad = nvkm_i2c_pad(port);
96         nv_ofuncs(pad)->fini(nv_object(pad), suspend);
97         return nvkm_object_fini(&port->base, suspend);
98 }
99
100 void
101 _nvkm_i2c_port_dtor(struct nvkm_object *object)
102 {
103         struct nvkm_i2c_port *port = (void *)object;
104         i2c_del_adapter(&port->adapter);
105         nvkm_object_destroy(&port->base);
106 }
107
108 int
109 nvkm_i2c_port_create_(struct nvkm_object *parent, struct nvkm_object *engine,
110                       struct nvkm_oclass *oclass, u8 index,
111                       const struct i2c_algorithm *algo,
112                       const struct nvkm_i2c_func *func,
113                       int size, void **pobject)
114 {
115         struct nvkm_device *device = nv_device(parent);
116         struct nvkm_i2c *i2c = nvkm_i2c(parent);
117         struct nvkm_i2c_port *port;
118         int ret;
119
120         ret = nvkm_object_create_(parent, engine, oclass, 0, size, pobject);
121         port = *pobject;
122         if (ret)
123                 return ret;
124
125         snprintf(port->adapter.name, sizeof(port->adapter.name),
126                  "nvkm-%s-%d", device->name, index);
127         port->adapter.owner = THIS_MODULE;
128         port->adapter.dev.parent = nv_device_base(device);
129         port->index = index;
130         port->aux = -1;
131         port->func = func;
132         mutex_init(&port->mutex);
133
134         if ( algo == &nvkm_i2c_bit_algo &&
135             !nvkm_boolopt(device->cfgopt, "NvI2C", CSTMSEL)) {
136                 struct i2c_algo_bit_data *bit;
137
138                 bit = kzalloc(sizeof(*bit), GFP_KERNEL);
139                 if (!bit)
140                         return -ENOMEM;
141
142                 bit->udelay = 10;
143                 bit->timeout = usecs_to_jiffies(2200);
144                 bit->data = port;
145                 bit->pre_xfer = nvkm_i2c_pre_xfer;
146                 bit->post_xfer = nvkm_i2c_post_xfer;
147                 bit->setsda = nvkm_i2c_setsda;
148                 bit->setscl = nvkm_i2c_setscl;
149                 bit->getsda = nvkm_i2c_getsda;
150                 bit->getscl = nvkm_i2c_getscl;
151
152                 port->adapter.algo_data = bit;
153                 ret = i2c_bit_add_bus(&port->adapter);
154         } else {
155                 port->adapter.algo_data = port;
156                 port->adapter.algo = algo;
157                 ret = i2c_add_adapter(&port->adapter);
158         }
159
160         if (ret == 0)
161                 list_add_tail(&port->head, &i2c->ports);
162         return ret;
163 }
164
165 /******************************************************************************
166  * base i2c subdev class implementation
167  *****************************************************************************/
168
169 static struct nvkm_i2c_port *
170 nvkm_i2c_find(struct nvkm_i2c *i2c, u8 index)
171 {
172         struct nvkm_bios *bios = nvkm_bios(i2c);
173         struct nvkm_i2c_port *port;
174
175         if (index == NV_I2C_DEFAULT(0) ||
176             index == NV_I2C_DEFAULT(1)) {
177                 u8  ver, hdr, cnt, len;
178                 u16 i2c = dcb_i2c_table(bios, &ver, &hdr, &cnt, &len);
179                 if (i2c && ver >= 0x30) {
180                         u8 auxidx = nv_ro08(bios, i2c + 4);
181                         if (index == NV_I2C_DEFAULT(0))
182                                 index = (auxidx & 0x0f) >> 0;
183                         else
184                                 index = (auxidx & 0xf0) >> 4;
185                 } else {
186                         index = 2;
187                 }
188         }
189
190         list_for_each_entry(port, &i2c->ports, head) {
191                 if (port->index == index)
192                         return port;
193         }
194
195         return NULL;
196 }
197
198 static struct nvkm_i2c_port *
199 nvkm_i2c_find_type(struct nvkm_i2c *i2c, u16 type)
200 {
201         struct nvkm_i2c_port *port;
202
203         list_for_each_entry(port, &i2c->ports, head) {
204                 if (nv_hclass(port) == type)
205                         return port;
206         }
207
208         return NULL;
209 }
210
211 static void
212 nvkm_i2c_release_pad(struct nvkm_i2c_port *port)
213 {
214         struct nvkm_i2c_pad *pad = nvkm_i2c_pad(port);
215         struct nvkm_i2c *i2c = nvkm_i2c(port);
216
217         if (atomic_dec_and_test(&nv_object(pad)->usecount)) {
218                 nv_ofuncs(pad)->fini(nv_object(pad), false);
219                 wake_up_all(&i2c->wait);
220         }
221 }
222
223 static int
224 nvkm_i2c_try_acquire_pad(struct nvkm_i2c_port *port)
225 {
226         struct nvkm_i2c_pad *pad = nvkm_i2c_pad(port);
227
228         if (atomic_add_return(1, &nv_object(pad)->usecount) != 1) {
229                 struct nvkm_object *owner = (void *)pad->port;
230                 do {
231                         if (owner == (void *)port)
232                                 return 0;
233                         owner = owner->parent;
234                 } while(owner);
235                 nvkm_i2c_release_pad(port);
236                 return -EBUSY;
237         }
238
239         pad->next = port;
240         nv_ofuncs(pad)->init(nv_object(pad));
241         return 0;
242 }
243
244 static int
245 nvkm_i2c_acquire_pad(struct nvkm_i2c_port *port, unsigned long timeout)
246 {
247         struct nvkm_i2c *i2c = nvkm_i2c(port);
248
249         if (timeout) {
250                 if (wait_event_timeout(i2c->wait,
251                                        nvkm_i2c_try_acquire_pad(port) == 0,
252                                        timeout) == 0)
253                         return -EBUSY;
254         } else {
255                 wait_event(i2c->wait, nvkm_i2c_try_acquire_pad(port) == 0);
256         }
257
258         return 0;
259 }
260
261 static void
262 nvkm_i2c_release(struct nvkm_i2c_port *port)
263 __releases(pad->mutex)
264 {
265         nvkm_i2c(port)->release_pad(port);
266         mutex_unlock(&port->mutex);
267 }
268
269 static int
270 nvkm_i2c_acquire(struct nvkm_i2c_port *port, unsigned long timeout)
271 __acquires(pad->mutex)
272 {
273         int ret;
274         mutex_lock(&port->mutex);
275         if ((ret = nvkm_i2c(port)->acquire_pad(port, timeout)))
276                 mutex_unlock(&port->mutex);
277         return ret;
278 }
279
280 static int
281 nvkm_i2c_identify(struct nvkm_i2c *i2c, int index, const char *what,
282                   struct nvkm_i2c_board_info *info,
283                   bool (*match)(struct nvkm_i2c_port *,
284                                 struct i2c_board_info *, void *), void *data)
285 {
286         struct nvkm_i2c_port *port = nvkm_i2c_find(i2c, index);
287         int i;
288
289         if (!port) {
290                 nv_debug(i2c, "no bus when probing %s on %d\n", what, index);
291                 return -ENODEV;
292         }
293
294         nv_debug(i2c, "probing %ss on bus: %d\n", what, port->index);
295         for (i = 0; info[i].dev.addr; i++) {
296                 u8 orig_udelay = 0;
297
298                 if ((port->adapter.algo == &i2c_bit_algo) &&
299                     (info[i].udelay != 0)) {
300                         struct i2c_algo_bit_data *algo = port->adapter.algo_data;
301                         nv_debug(i2c, "using custom udelay %d instead of %d\n",
302                                  info[i].udelay, algo->udelay);
303                         orig_udelay = algo->udelay;
304                         algo->udelay = info[i].udelay;
305                 }
306
307                 if (nv_probe_i2c(port, info[i].dev.addr) &&
308                     (!match || match(port, &info[i].dev, data))) {
309                         nv_info(i2c, "detected %s: %s\n", what,
310                                 info[i].dev.type);
311                         return i;
312                 }
313
314                 if (orig_udelay) {
315                         struct i2c_algo_bit_data *algo = port->adapter.algo_data;
316                         algo->udelay = orig_udelay;
317                 }
318         }
319
320         nv_debug(i2c, "no devices found.\n");
321         return -ENODEV;
322 }
323
324 static void
325 nvkm_i2c_intr_fini(struct nvkm_event *event, int type, int index)
326 {
327         struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
328         struct nvkm_i2c_port *port = i2c->find(i2c, index);
329         const struct nvkm_i2c_impl *impl = (void *)nv_object(i2c)->oclass;
330         if (port && port->aux >= 0)
331                 impl->aux_mask(i2c, type, 1 << port->aux, 0);
332 }
333
334 static void
335 nvkm_i2c_intr_init(struct nvkm_event *event, int type, int index)
336 {
337         struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
338         struct nvkm_i2c_port *port = i2c->find(i2c, index);
339         const struct nvkm_i2c_impl *impl = (void *)nv_object(i2c)->oclass;
340         if (port && port->aux >= 0)
341                 impl->aux_mask(i2c, type, 1 << port->aux, 1 << port->aux);
342 }
343
344 static int
345 nvkm_i2c_intr_ctor(struct nvkm_object *object, void *data, u32 size,
346                       struct nvkm_notify *notify)
347 {
348         struct nvkm_i2c_ntfy_req *req = data;
349         if (!WARN_ON(size != sizeof(*req))) {
350                 notify->size  = sizeof(struct nvkm_i2c_ntfy_rep);
351                 notify->types = req->mask;
352                 notify->index = req->port;
353                 return 0;
354         }
355         return -EINVAL;
356 }
357
358 static void
359 nvkm_i2c_intr(struct nvkm_subdev *subdev)
360 {
361         struct nvkm_i2c_impl *impl = (void *)nv_oclass(subdev);
362         struct nvkm_i2c *i2c = nvkm_i2c(subdev);
363         struct nvkm_i2c_port *port;
364         u32 hi, lo, rq, tx, e;
365
366         if (impl->aux_stat) {
367                 impl->aux_stat(i2c, &hi, &lo, &rq, &tx);
368                 if (hi || lo || rq || tx) {
369                         list_for_each_entry(port, &i2c->ports, head) {
370                                 if (e = 0, port->aux < 0)
371                                         continue;
372
373                                 if (hi & (1 << port->aux)) e |= NVKM_I2C_PLUG;
374                                 if (lo & (1 << port->aux)) e |= NVKM_I2C_UNPLUG;
375                                 if (rq & (1 << port->aux)) e |= NVKM_I2C_IRQ;
376                                 if (tx & (1 << port->aux)) e |= NVKM_I2C_DONE;
377                                 if (e) {
378                                         struct nvkm_i2c_ntfy_rep rep = {
379                                                 .mask = e,
380                                         };
381                                         nvkm_event_send(&i2c->event, rep.mask,
382                                                         port->index, &rep,
383                                                         sizeof(rep));
384                                 }
385                         }
386                 }
387         }
388 }
389
390 static const struct nvkm_event_func
391 nvkm_i2c_intr_func = {
392         .ctor = nvkm_i2c_intr_ctor,
393         .init = nvkm_i2c_intr_init,
394         .fini = nvkm_i2c_intr_fini,
395 };
396
397 int
398 _nvkm_i2c_fini(struct nvkm_object *object, bool suspend)
399 {
400         struct nvkm_i2c_impl *impl = (void *)nv_oclass(object);
401         struct nvkm_i2c *i2c = (void *)object;
402         struct nvkm_i2c_port *port;
403         u32 mask;
404         int ret;
405
406         list_for_each_entry(port, &i2c->ports, head) {
407                 ret = nv_ofuncs(port)->fini(nv_object(port), suspend);
408                 if (ret && suspend)
409                         goto fail;
410         }
411
412         if ((mask = (1 << impl->aux) - 1), impl->aux_stat) {
413                 impl->aux_mask(i2c, NVKM_I2C_ANY, mask, 0);
414                 impl->aux_stat(i2c, &mask, &mask, &mask, &mask);
415         }
416
417         return nvkm_subdev_fini(&i2c->base, suspend);
418 fail:
419         list_for_each_entry_continue_reverse(port, &i2c->ports, head) {
420                 nv_ofuncs(port)->init(nv_object(port));
421         }
422
423         return ret;
424 }
425
426 int
427 _nvkm_i2c_init(struct nvkm_object *object)
428 {
429         struct nvkm_i2c *i2c = (void *)object;
430         struct nvkm_i2c_port *port;
431         int ret;
432
433         ret = nvkm_subdev_init(&i2c->base);
434         if (ret == 0) {
435                 list_for_each_entry(port, &i2c->ports, head) {
436                         ret = nv_ofuncs(port)->init(nv_object(port));
437                         if (ret)
438                                 goto fail;
439                 }
440         }
441
442         return ret;
443 fail:
444         list_for_each_entry_continue_reverse(port, &i2c->ports, head) {
445                 nv_ofuncs(port)->fini(nv_object(port), false);
446         }
447
448         return ret;
449 }
450
451 void
452 _nvkm_i2c_dtor(struct nvkm_object *object)
453 {
454         struct nvkm_i2c *i2c = (void *)object;
455         struct nvkm_i2c_port *port, *temp;
456
457         nvkm_event_fini(&i2c->event);
458
459         list_for_each_entry_safe(port, temp, &i2c->ports, head) {
460                 nvkm_object_ref(NULL, (struct nvkm_object **)&port);
461         }
462
463         nvkm_subdev_destroy(&i2c->base);
464 }
465
466 static struct nvkm_oclass *
467 nvkm_i2c_extdev_sclass[] = {
468         nvkm_anx9805_sclass,
469 };
470
471 static void
472 nvkm_i2c_create_port(struct nvkm_i2c *i2c, int index, u8 type,
473                      struct dcb_i2c_entry *info)
474 {
475         const struct nvkm_i2c_impl *impl = (void *)nv_oclass(i2c);
476         struct nvkm_oclass *oclass;
477         struct nvkm_object *parent;
478         struct nvkm_object *object;
479         int ret, pad;
480
481         if (info->share != DCB_I2C_UNUSED) {
482                 pad    = info->share;
483                 oclass = impl->pad_s;
484         } else {
485                 if (type != DCB_I2C_NVIO_AUX)
486                         pad = 0x100 + info->drive;
487                 else
488                         pad = 0x100 + info->auxch;
489                 oclass = impl->pad_x;
490         }
491
492         ret = nvkm_object_ctor(nv_object(i2c), NULL, oclass,
493                                NULL, pad, &parent);
494         if (ret < 0)
495                 return;
496
497         oclass = impl->sclass;
498         do {
499                 ret = -EINVAL;
500                 if (oclass->handle == type) {
501                         ret = nvkm_object_ctor(parent, NULL, oclass,
502                                                info, index, &object);
503                 }
504         } while (ret && (++oclass)->handle);
505
506         nvkm_object_ref(NULL, &parent);
507 }
508
509 int
510 nvkm_i2c_create_(struct nvkm_object *parent, struct nvkm_object *engine,
511                  struct nvkm_oclass *oclass, int length, void **pobject)
512 {
513         struct nvkm_bios *bios = nvkm_bios(parent);
514         struct nvkm_i2c *i2c;
515         struct nvkm_object *object;
516         struct dcb_i2c_entry info;
517         int ret, i, j, index = -1;
518         struct dcb_output outp;
519         u8  ver, hdr;
520         u32 data;
521
522         ret = nvkm_subdev_create(parent, engine, oclass, 0, "I2C", "i2c", &i2c);
523         *pobject = nv_object(i2c);
524         if (ret)
525                 return ret;
526
527         nv_subdev(i2c)->intr = nvkm_i2c_intr;
528         i2c->find = nvkm_i2c_find;
529         i2c->find_type = nvkm_i2c_find_type;
530         i2c->acquire_pad = nvkm_i2c_acquire_pad;
531         i2c->release_pad = nvkm_i2c_release_pad;
532         i2c->acquire = nvkm_i2c_acquire;
533         i2c->release = nvkm_i2c_release;
534         i2c->identify = nvkm_i2c_identify;
535         init_waitqueue_head(&i2c->wait);
536         INIT_LIST_HEAD(&i2c->ports);
537
538         while (!dcb_i2c_parse(bios, ++index, &info)) {
539                 switch (info.type) {
540                 case DCB_I2C_NV04_BIT:
541                 case DCB_I2C_NV4E_BIT:
542                 case DCB_I2C_NVIO_BIT:
543                         nvkm_i2c_create_port(i2c, NV_I2C_PORT(index),
544                                              info.type, &info);
545                         break;
546                 case DCB_I2C_NVIO_AUX:
547                         nvkm_i2c_create_port(i2c, NV_I2C_AUX(index),
548                                              info.type, &info);
549                         break;
550                 case DCB_I2C_PMGR:
551                         if (info.drive != DCB_I2C_UNUSED) {
552                                 nvkm_i2c_create_port(i2c, NV_I2C_PORT(index),
553                                                      DCB_I2C_NVIO_BIT, &info);
554                         }
555                         if (info.auxch != DCB_I2C_UNUSED) {
556                                 nvkm_i2c_create_port(i2c, NV_I2C_AUX(index),
557                                                      DCB_I2C_NVIO_AUX, &info);
558                         }
559                         break;
560                 case DCB_I2C_UNUSED:
561                 default:
562                         continue;
563                 }
564         }
565
566         /* in addition to the busses specified in the i2c table, there
567          * may be ddc/aux channels hiding behind external tmds/dp/etc
568          * transmitters.
569          */
570         index = NV_I2C_EXT(0);
571         i = -1;
572         while ((data = dcb_outp_parse(bios, ++i, &ver, &hdr, &outp))) {
573                 if (!outp.location || !outp.extdev)
574                         continue;
575
576                 switch (outp.type) {
577                 case DCB_OUTPUT_TMDS:
578                         info.type = NV_I2C_TYPE_EXTDDC(outp.extdev);
579                         break;
580                 case DCB_OUTPUT_DP:
581                         info.type = NV_I2C_TYPE_EXTAUX(outp.extdev);
582                         break;
583                 default:
584                         continue;
585                 }
586
587                 ret = -ENODEV;
588                 j = -1;
589                 while (ret && ++j < ARRAY_SIZE(nvkm_i2c_extdev_sclass)) {
590                         parent = nv_object(i2c->find(i2c, outp.i2c_index));
591                         oclass = nvkm_i2c_extdev_sclass[j];
592                         do {
593                                 if (oclass->handle != info.type)
594                                         continue;
595                                 ret = nvkm_object_ctor(parent, NULL, oclass,
596                                                        NULL, index++, &object);
597                         } while (ret && (++oclass)->handle);
598                 }
599         }
600
601         ret = nvkm_event_init(&nvkm_i2c_intr_func, 4, index, &i2c->event);
602         if (ret)
603                 return ret;
604
605         return 0;
606 }
607
608 int
609 _nvkm_i2c_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
610                struct nvkm_oclass *oclass, void *data, u32 size,
611                struct nvkm_object **pobject)
612 {
613         struct nvkm_i2c *i2c;
614         int ret;
615
616         ret = nvkm_i2c_create(parent, engine, oclass, &i2c);
617         *pobject = nv_object(i2c);
618         if (ret)
619                 return ret;
620
621         return 0;
622 }