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