]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/nouveau/nvkm/subdev/i2c/base.c
ecacb22834d76d24c9ee656748715ca552ed10ae
[linux.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 "aux.h"
26 #include "bus.h"
27 #include "pad.h"
28
29 #include <core/notify.h>
30 #include <core/option.h>
31 #include <subdev/bios.h>
32 #include <subdev/bios/dcb.h>
33 #include <subdev/bios/i2c.h>
34
35 static struct nvkm_i2c_pad *
36 nvkm_i2c_pad_find(struct nvkm_i2c *i2c, int id)
37 {
38         struct nvkm_i2c_pad *pad;
39
40         list_for_each_entry(pad, &i2c->pad, head) {
41                 if (pad->id == id)
42                         return pad;
43         }
44
45         return NULL;
46 }
47
48 struct nvkm_i2c_bus *
49 nvkm_i2c_bus_find(struct nvkm_i2c *i2c, int id)
50 {
51         struct nvkm_bios *bios = i2c->subdev.device->bios;
52         struct nvkm_i2c_bus *bus;
53
54         if (id == NVKM_I2C_BUS_PRI || id == NVKM_I2C_BUS_SEC) {
55                 u8  ver, hdr, cnt, len;
56                 u16 i2c = dcb_i2c_table(bios, &ver, &hdr, &cnt, &len);
57                 if (i2c && ver >= 0x30) {
58                         u8 auxidx = nvbios_rd08(bios, i2c + 4);
59                         if (id == NVKM_I2C_BUS_PRI)
60                                 id = NVKM_I2C_BUS_CCB((auxidx & 0x0f) >> 0);
61                         else
62                                 id = NVKM_I2C_BUS_CCB((auxidx & 0xf0) >> 4);
63                 } else {
64                         id = NVKM_I2C_BUS_CCB(2);
65                 }
66         }
67
68         list_for_each_entry(bus, &i2c->bus, head) {
69                 if (bus->id == id)
70                         return bus;
71         }
72
73         return NULL;
74 }
75
76 struct nvkm_i2c_aux *
77 nvkm_i2c_aux_find(struct nvkm_i2c *i2c, int id)
78 {
79         struct nvkm_i2c_aux *aux;
80
81         list_for_each_entry(aux, &i2c->aux, head) {
82                 if (aux->id == id)
83                         return aux;
84         }
85
86         return NULL;
87 }
88
89 static void
90 nvkm_i2c_intr_fini(struct nvkm_event *event, int type, int id)
91 {
92         struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
93         struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
94         if (aux)
95                 i2c->func->aux_mask(i2c, type, aux->intr, 0);
96 }
97
98 static void
99 nvkm_i2c_intr_init(struct nvkm_event *event, int type, int id)
100 {
101         struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
102         struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
103         if (aux)
104                 i2c->func->aux_mask(i2c, type, aux->intr, aux->intr);
105 }
106
107 static int
108 nvkm_i2c_intr_ctor(struct nvkm_object *object, void *data, u32 size,
109                    struct nvkm_notify *notify)
110 {
111         struct nvkm_i2c_ntfy_req *req = data;
112         if (!WARN_ON(size != sizeof(*req))) {
113                 notify->size  = sizeof(struct nvkm_i2c_ntfy_rep);
114                 notify->types = req->mask;
115                 notify->index = req->port;
116                 return 0;
117         }
118         return -EINVAL;
119 }
120
121 static const struct nvkm_event_func
122 nvkm_i2c_intr_func = {
123         .ctor = nvkm_i2c_intr_ctor,
124         .init = nvkm_i2c_intr_init,
125         .fini = nvkm_i2c_intr_fini,
126 };
127
128 static void
129 nvkm_i2c_intr(struct nvkm_subdev *subdev)
130 {
131         struct nvkm_i2c *i2c = nvkm_i2c(subdev);
132         struct nvkm_i2c_aux *aux;
133         u32 hi, lo, rq, tx;
134
135         if (!i2c->func->aux_stat)
136                 return;
137
138         i2c->func->aux_stat(i2c, &hi, &lo, &rq, &tx);
139         if (!hi && !lo && !rq && !tx)
140                 return;
141
142         list_for_each_entry(aux, &i2c->aux, head) {
143                 u32 mask = 0;
144                 if (hi & aux->intr) mask |= NVKM_I2C_PLUG;
145                 if (lo & aux->intr) mask |= NVKM_I2C_UNPLUG;
146                 if (rq & aux->intr) mask |= NVKM_I2C_IRQ;
147                 if (tx & aux->intr) mask |= NVKM_I2C_DONE;
148                 if (mask) {
149                         struct nvkm_i2c_ntfy_rep rep = {
150                                 .mask = mask,
151                         };
152                         nvkm_event_send(&i2c->event, rep.mask, aux->id,
153                                         &rep, sizeof(rep));
154                 }
155         }
156 }
157
158 static int
159 nvkm_i2c_fini(struct nvkm_subdev *subdev, bool suspend)
160 {
161         struct nvkm_i2c *i2c = nvkm_i2c(subdev);
162         struct nvkm_i2c_pad *pad;
163         struct nvkm_i2c_bus *bus;
164         struct nvkm_i2c_aux *aux;
165         u32 mask;
166
167         list_for_each_entry(aux, &i2c->aux, head) {
168                 nvkm_i2c_aux_fini(aux);
169         }
170
171         list_for_each_entry(bus, &i2c->bus, head) {
172                 nvkm_i2c_bus_fini(bus);
173         }
174
175         if ((mask = (1 << i2c->func->aux) - 1), i2c->func->aux_stat) {
176                 i2c->func->aux_mask(i2c, NVKM_I2C_ANY, mask, 0);
177                 i2c->func->aux_stat(i2c, &mask, &mask, &mask, &mask);
178         }
179
180         list_for_each_entry(pad, &i2c->pad, head) {
181                 nvkm_i2c_pad_fini(pad);
182         }
183
184         return 0;
185 }
186
187 static int
188 nvkm_i2c_init(struct nvkm_subdev *subdev)
189 {
190         struct nvkm_i2c *i2c = nvkm_i2c(subdev);
191         struct nvkm_i2c_bus *bus;
192         struct nvkm_i2c_pad *pad;
193         struct nvkm_i2c_aux *aux;
194
195         list_for_each_entry(pad, &i2c->pad, head) {
196                 nvkm_i2c_pad_init(pad);
197         }
198
199         list_for_each_entry(bus, &i2c->bus, head) {
200                 nvkm_i2c_bus_init(bus);
201         }
202
203         list_for_each_entry(aux, &i2c->aux, head) {
204                 nvkm_i2c_aux_init(aux);
205         }
206
207         return 0;
208 }
209
210 static void *
211 nvkm_i2c_dtor(struct nvkm_subdev *subdev)
212 {
213         struct nvkm_i2c *i2c = nvkm_i2c(subdev);
214
215         nvkm_event_fini(&i2c->event);
216
217         while (!list_empty(&i2c->aux)) {
218                 struct nvkm_i2c_aux *aux =
219                         list_first_entry(&i2c->aux, typeof(*aux), head);
220                 nvkm_i2c_aux_del(&aux);
221         }
222
223         while (!list_empty(&i2c->bus)) {
224                 struct nvkm_i2c_bus *bus =
225                         list_first_entry(&i2c->bus, typeof(*bus), head);
226                 nvkm_i2c_bus_del(&bus);
227         }
228
229         while (!list_empty(&i2c->pad)) {
230                 struct nvkm_i2c_pad *pad =
231                         list_first_entry(&i2c->pad, typeof(*pad), head);
232                 nvkm_i2c_pad_del(&pad);
233         }
234
235         return i2c;
236 }
237
238 static const struct nvkm_subdev_func
239 nvkm_i2c = {
240         .dtor = nvkm_i2c_dtor,
241         .init = nvkm_i2c_init,
242         .fini = nvkm_i2c_fini,
243         .intr = nvkm_i2c_intr,
244 };
245
246 static const struct nvkm_i2c_drv {
247         u8 bios;
248         u8 addr;
249         int (*pad_new)(struct nvkm_i2c_bus *, int id, u8 addr,
250                        struct nvkm_i2c_pad **);
251 }
252 nvkm_i2c_drv[] = {
253         { 0x0d, 0x39, anx9805_pad_new },
254         { 0x0e, 0x3b, anx9805_pad_new },
255         {}
256 };
257
258 int
259 nvkm_i2c_new_(const struct nvkm_i2c_func *func, struct nvkm_device *device,
260               int index, struct nvkm_i2c **pi2c)
261 {
262         struct nvkm_bios *bios = device->bios;
263         struct nvkm_i2c *i2c;
264         struct dcb_i2c_entry ccbE;
265         struct dcb_output dcbE;
266         u8 ver, hdr;
267         int ret, i;
268
269         if (!(i2c = *pi2c = kzalloc(sizeof(*i2c), GFP_KERNEL)))
270                 return -ENOMEM;
271
272         nvkm_subdev_ctor(&nvkm_i2c, device, index, &i2c->subdev);
273         i2c->func = func;
274         INIT_LIST_HEAD(&i2c->pad);
275         INIT_LIST_HEAD(&i2c->bus);
276         INIT_LIST_HEAD(&i2c->aux);
277
278         i = -1;
279         while (!dcb_i2c_parse(bios, ++i, &ccbE)) {
280                 struct nvkm_i2c_pad *pad = NULL;
281                 struct nvkm_i2c_bus *bus = NULL;
282                 struct nvkm_i2c_aux *aux = NULL;
283
284                 nvkm_debug(&i2c->subdev, "ccb %02x: type %02x drive %02x "
285                            "sense %02x share %02x auxch %02x\n", i, ccbE.type,
286                            ccbE.drive, ccbE.sense, ccbE.share, ccbE.auxch);
287
288                 if (ccbE.share != DCB_I2C_UNUSED) {
289                         const int id = NVKM_I2C_PAD_HYBRID(ccbE.share);
290                         if (!(pad = nvkm_i2c_pad_find(i2c, id)))
291                                 ret = func->pad_s_new(i2c, id, &pad);
292                         else
293                                 ret = 0;
294                 } else {
295                         ret = func->pad_x_new(i2c, NVKM_I2C_PAD_CCB(i), &pad);
296                 }
297
298                 if (ret) {
299                         nvkm_error(&i2c->subdev, "ccb %02x pad, %d\n", i, ret);
300                         nvkm_i2c_pad_del(&pad);
301                         continue;
302                 }
303
304                 if (pad->func->bus_new_0 && ccbE.type == DCB_I2C_NV04_BIT) {
305                         ret = pad->func->bus_new_0(pad, NVKM_I2C_BUS_CCB(i),
306                                                    ccbE.drive,
307                                                    ccbE.sense, &bus);
308                 } else
309                 if (pad->func->bus_new_4 &&
310                     ( ccbE.type == DCB_I2C_NV4E_BIT ||
311                       ccbE.type == DCB_I2C_NVIO_BIT ||
312                      (ccbE.type == DCB_I2C_PMGR &&
313                       ccbE.drive != DCB_I2C_UNUSED))) {
314                         ret = pad->func->bus_new_4(pad, NVKM_I2C_BUS_CCB(i),
315                                                    ccbE.drive, &bus);
316                 }
317
318                 if (ret) {
319                         nvkm_error(&i2c->subdev, "ccb %02x bus, %d\n", i, ret);
320                         nvkm_i2c_bus_del(&bus);
321                 }
322
323                 if (pad->func->aux_new_6 &&
324                     ( ccbE.type == DCB_I2C_NVIO_AUX ||
325                      (ccbE.type == DCB_I2C_PMGR &&
326                       ccbE.auxch != DCB_I2C_UNUSED))) {
327                         ret = pad->func->aux_new_6(pad, NVKM_I2C_BUS_CCB(i),
328                                                    ccbE.auxch, &aux);
329                 } else {
330                         ret = 0;
331                 }
332
333                 if (ret) {
334                         nvkm_error(&i2c->subdev, "ccb %02x aux, %d\n", i, ret);
335                         nvkm_i2c_aux_del(&aux);
336                 }
337
338                 if (ccbE.type != DCB_I2C_UNUSED && !bus && !aux) {
339                         nvkm_warn(&i2c->subdev, "ccb %02x was ignored\n", i);
340                         continue;
341                 }
342         }
343
344         i = -1;
345         while (dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE)) {
346                 const struct nvkm_i2c_drv *drv = nvkm_i2c_drv;
347                 struct nvkm_i2c_bus *bus;
348                 struct nvkm_i2c_pad *pad;
349
350                 /* internal outputs handled by native i2c busses (above) */
351                 if (!dcbE.location)
352                         continue;
353
354                 /* we need an i2c bus to talk to the external encoder */
355                 bus = nvkm_i2c_bus_find(i2c, dcbE.i2c_index);
356                 if (!bus) {
357                         nvkm_debug(&i2c->subdev, "dcb %02x no bus\n", i);
358                         continue;
359                 }
360
361                 /* ... and a driver for it */
362                 while (drv->pad_new) {
363                         if (drv->bios == dcbE.extdev)
364                                 break;
365                         drv++;
366                 }
367
368                 if (!drv->pad_new) {
369                         nvkm_debug(&i2c->subdev, "dcb %02x drv %02x unknown\n",
370                                    i, dcbE.extdev);
371                         continue;
372                 }
373
374                 /* find/create an instance of the driver */
375                 pad = nvkm_i2c_pad_find(i2c, NVKM_I2C_PAD_EXT(dcbE.extdev));
376                 if (!pad) {
377                         const int id = NVKM_I2C_PAD_EXT(dcbE.extdev);
378                         ret = drv->pad_new(bus, id, drv->addr, &pad);
379                         if (ret) {
380                                 nvkm_error(&i2c->subdev, "dcb %02x pad, %d\n",
381                                            i, ret);
382                                 nvkm_i2c_pad_del(&pad);
383                                 continue;
384                         }
385                 }
386
387                 /* create any i2c bus / aux channel required by the output */
388                 if (pad->func->aux_new_6 && dcbE.type == DCB_OUTPUT_DP) {
389                         const int id = NVKM_I2C_AUX_EXT(dcbE.extdev);
390                         struct nvkm_i2c_aux *aux = NULL;
391                         ret = pad->func->aux_new_6(pad, id, 0, &aux);
392                         if (ret) {
393                                 nvkm_error(&i2c->subdev, "dcb %02x aux, %d\n",
394                                            i, ret);
395                                 nvkm_i2c_aux_del(&aux);
396                         }
397                 } else
398                 if (pad->func->bus_new_4) {
399                         const int id = NVKM_I2C_BUS_EXT(dcbE.extdev);
400                         struct nvkm_i2c_bus *bus = NULL;
401                         ret = pad->func->bus_new_4(pad, id, 0, &bus);
402                         if (ret) {
403                                 nvkm_error(&i2c->subdev, "dcb %02x bus, %d\n",
404                                            i, ret);
405                                 nvkm_i2c_bus_del(&bus);
406                         }
407                 }
408         }
409
410         return nvkm_event_init(&nvkm_i2c_intr_func, 4, i, &i2c->event);
411 }