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