Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright 2013 Red Hat Inc.
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the "Software"),
0006  * to deal in the Software without restriction, including without limitation
0007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0008  * and/or sell copies of the Software, and to permit persons to whom the
0009  * Software is furnished to do so, subject to the following conditions:
0010  *
0011  * The above copyright notice and this permission notice shall be included in
0012  * all copies or substantial portions of the Software.
0013  *
0014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0017  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020  * OTHER DEALINGS IN THE SOFTWARE.
0021  *
0022  * Authors: Ben Skeggs
0023  */
0024 #include "priv.h"
0025 #include "aux.h"
0026 #include "bus.h"
0027 #include "pad.h"
0028 
0029 #include <core/notify.h>
0030 #include <core/option.h>
0031 #include <subdev/bios.h>
0032 #include <subdev/bios/dcb.h>
0033 #include <subdev/bios/i2c.h>
0034 
0035 static struct nvkm_i2c_pad *
0036 nvkm_i2c_pad_find(struct nvkm_i2c *i2c, int id)
0037 {
0038     struct nvkm_i2c_pad *pad;
0039 
0040     list_for_each_entry(pad, &i2c->pad, head) {
0041         if (pad->id == id)
0042             return pad;
0043     }
0044 
0045     return NULL;
0046 }
0047 
0048 struct nvkm_i2c_bus *
0049 nvkm_i2c_bus_find(struct nvkm_i2c *i2c, int id)
0050 {
0051     struct nvkm_bios *bios = i2c->subdev.device->bios;
0052     struct nvkm_i2c_bus *bus;
0053 
0054     if (id == NVKM_I2C_BUS_PRI || id == NVKM_I2C_BUS_SEC) {
0055         u8  ver, hdr, cnt, len;
0056         u16 i2c = dcb_i2c_table(bios, &ver, &hdr, &cnt, &len);
0057         if (i2c && ver >= 0x30) {
0058             u8 auxidx = nvbios_rd08(bios, i2c + 4);
0059             if (id == NVKM_I2C_BUS_PRI)
0060                 id = NVKM_I2C_BUS_CCB((auxidx & 0x0f) >> 0);
0061             else
0062                 id = NVKM_I2C_BUS_CCB((auxidx & 0xf0) >> 4);
0063         } else {
0064             id = NVKM_I2C_BUS_CCB(2);
0065         }
0066     }
0067 
0068     list_for_each_entry(bus, &i2c->bus, head) {
0069         if (bus->id == id)
0070             return bus;
0071     }
0072 
0073     return NULL;
0074 }
0075 
0076 struct nvkm_i2c_aux *
0077 nvkm_i2c_aux_find(struct nvkm_i2c *i2c, int id)
0078 {
0079     struct nvkm_i2c_aux *aux;
0080 
0081     list_for_each_entry(aux, &i2c->aux, head) {
0082         if (aux->id == id)
0083             return aux;
0084     }
0085 
0086     return NULL;
0087 }
0088 
0089 static void
0090 nvkm_i2c_intr_fini(struct nvkm_event *event, int type, int id)
0091 {
0092     struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
0093     struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
0094     if (aux)
0095         i2c->func->aux_mask(i2c, type, aux->intr, 0);
0096 }
0097 
0098 static void
0099 nvkm_i2c_intr_init(struct nvkm_event *event, int type, int id)
0100 {
0101     struct nvkm_i2c *i2c = container_of(event, typeof(*i2c), event);
0102     struct nvkm_i2c_aux *aux = nvkm_i2c_aux_find(i2c, id);
0103     if (aux)
0104         i2c->func->aux_mask(i2c, type, aux->intr, aux->intr);
0105 }
0106 
0107 static int
0108 nvkm_i2c_intr_ctor(struct nvkm_object *object, void *data, u32 size,
0109            struct nvkm_notify *notify)
0110 {
0111     struct nvkm_i2c_ntfy_req *req = data;
0112     if (!WARN_ON(size != sizeof(*req))) {
0113         notify->size  = sizeof(struct nvkm_i2c_ntfy_rep);
0114         notify->types = req->mask;
0115         notify->index = req->port;
0116         return 0;
0117     }
0118     return -EINVAL;
0119 }
0120 
0121 static const struct nvkm_event_func
0122 nvkm_i2c_intr_func = {
0123     .ctor = nvkm_i2c_intr_ctor,
0124     .init = nvkm_i2c_intr_init,
0125     .fini = nvkm_i2c_intr_fini,
0126 };
0127 
0128 static void
0129 nvkm_i2c_intr(struct nvkm_subdev *subdev)
0130 {
0131     struct nvkm_i2c *i2c = nvkm_i2c(subdev);
0132     struct nvkm_i2c_aux *aux;
0133     u32 hi, lo, rq, tx;
0134 
0135     if (!i2c->func->aux_stat)
0136         return;
0137 
0138     i2c->func->aux_stat(i2c, &hi, &lo, &rq, &tx);
0139     if (!hi && !lo && !rq && !tx)
0140         return;
0141 
0142     list_for_each_entry(aux, &i2c->aux, head) {
0143         u32 mask = 0;
0144         if (hi & aux->intr) mask |= NVKM_I2C_PLUG;
0145         if (lo & aux->intr) mask |= NVKM_I2C_UNPLUG;
0146         if (rq & aux->intr) mask |= NVKM_I2C_IRQ;
0147         if (tx & aux->intr) mask |= NVKM_I2C_DONE;
0148         if (mask) {
0149             struct nvkm_i2c_ntfy_rep rep = {
0150                 .mask = mask,
0151             };
0152             nvkm_event_send(&i2c->event, rep.mask, aux->id,
0153                     &rep, sizeof(rep));
0154         }
0155     }
0156 }
0157 
0158 static int
0159 nvkm_i2c_fini(struct nvkm_subdev *subdev, bool suspend)
0160 {
0161     struct nvkm_i2c *i2c = nvkm_i2c(subdev);
0162     struct nvkm_i2c_pad *pad;
0163     struct nvkm_i2c_bus *bus;
0164     struct nvkm_i2c_aux *aux;
0165     u32 mask;
0166 
0167     list_for_each_entry(aux, &i2c->aux, head) {
0168         nvkm_i2c_aux_fini(aux);
0169     }
0170 
0171     list_for_each_entry(bus, &i2c->bus, head) {
0172         nvkm_i2c_bus_fini(bus);
0173     }
0174 
0175     if ((mask = (1 << i2c->func->aux) - 1), i2c->func->aux_stat) {
0176         i2c->func->aux_mask(i2c, NVKM_I2C_ANY, mask, 0);
0177         i2c->func->aux_stat(i2c, &mask, &mask, &mask, &mask);
0178     }
0179 
0180     list_for_each_entry(pad, &i2c->pad, head) {
0181         nvkm_i2c_pad_fini(pad);
0182     }
0183 
0184     return 0;
0185 }
0186 
0187 static int
0188 nvkm_i2c_preinit(struct nvkm_subdev *subdev)
0189 {
0190     struct nvkm_i2c *i2c = nvkm_i2c(subdev);
0191     struct nvkm_i2c_bus *bus;
0192     struct nvkm_i2c_pad *pad;
0193 
0194     /*
0195      * We init our i2c busses as early as possible, since they may be
0196      * needed by the vbios init scripts on some cards
0197      */
0198     list_for_each_entry(pad, &i2c->pad, head)
0199         nvkm_i2c_pad_init(pad);
0200     list_for_each_entry(bus, &i2c->bus, head)
0201         nvkm_i2c_bus_init(bus);
0202 
0203     return 0;
0204 }
0205 
0206 static int
0207 nvkm_i2c_init(struct nvkm_subdev *subdev)
0208 {
0209     struct nvkm_i2c *i2c = nvkm_i2c(subdev);
0210     struct nvkm_i2c_bus *bus;
0211     struct nvkm_i2c_pad *pad;
0212     struct nvkm_i2c_aux *aux;
0213 
0214     list_for_each_entry(pad, &i2c->pad, head) {
0215         nvkm_i2c_pad_init(pad);
0216     }
0217 
0218     list_for_each_entry(bus, &i2c->bus, head) {
0219         nvkm_i2c_bus_init(bus);
0220     }
0221 
0222     list_for_each_entry(aux, &i2c->aux, head) {
0223         nvkm_i2c_aux_init(aux);
0224     }
0225 
0226     return 0;
0227 }
0228 
0229 static void *
0230 nvkm_i2c_dtor(struct nvkm_subdev *subdev)
0231 {
0232     struct nvkm_i2c *i2c = nvkm_i2c(subdev);
0233 
0234     nvkm_event_fini(&i2c->event);
0235 
0236     while (!list_empty(&i2c->aux)) {
0237         struct nvkm_i2c_aux *aux =
0238             list_first_entry(&i2c->aux, typeof(*aux), head);
0239         nvkm_i2c_aux_del(&aux);
0240     }
0241 
0242     while (!list_empty(&i2c->bus)) {
0243         struct nvkm_i2c_bus *bus =
0244             list_first_entry(&i2c->bus, typeof(*bus), head);
0245         nvkm_i2c_bus_del(&bus);
0246     }
0247 
0248     while (!list_empty(&i2c->pad)) {
0249         struct nvkm_i2c_pad *pad =
0250             list_first_entry(&i2c->pad, typeof(*pad), head);
0251         nvkm_i2c_pad_del(&pad);
0252     }
0253 
0254     return i2c;
0255 }
0256 
0257 static const struct nvkm_subdev_func
0258 nvkm_i2c = {
0259     .dtor = nvkm_i2c_dtor,
0260     .preinit = nvkm_i2c_preinit,
0261     .init = nvkm_i2c_init,
0262     .fini = nvkm_i2c_fini,
0263     .intr = nvkm_i2c_intr,
0264 };
0265 
0266 static const struct nvkm_i2c_drv {
0267     u8 bios;
0268     u8 addr;
0269     int (*pad_new)(struct nvkm_i2c_bus *, int id, u8 addr,
0270                struct nvkm_i2c_pad **);
0271 }
0272 nvkm_i2c_drv[] = {
0273     { 0x0d, 0x39, anx9805_pad_new },
0274     { 0x0e, 0x3b, anx9805_pad_new },
0275     {}
0276 };
0277 
0278 int
0279 nvkm_i2c_new_(const struct nvkm_i2c_func *func, struct nvkm_device *device,
0280           enum nvkm_subdev_type type, int inst, struct nvkm_i2c **pi2c)
0281 {
0282     struct nvkm_bios *bios = device->bios;
0283     struct nvkm_i2c *i2c;
0284     struct dcb_i2c_entry ccbE;
0285     struct dcb_output dcbE;
0286     u8 ver, hdr;
0287     int ret, i;
0288 
0289     if (!(i2c = *pi2c = kzalloc(sizeof(*i2c), GFP_KERNEL)))
0290         return -ENOMEM;
0291 
0292     nvkm_subdev_ctor(&nvkm_i2c, device, type, inst, &i2c->subdev);
0293     i2c->func = func;
0294     INIT_LIST_HEAD(&i2c->pad);
0295     INIT_LIST_HEAD(&i2c->bus);
0296     INIT_LIST_HEAD(&i2c->aux);
0297 
0298     i = -1;
0299     while (!dcb_i2c_parse(bios, ++i, &ccbE)) {
0300         struct nvkm_i2c_pad *pad = NULL;
0301         struct nvkm_i2c_bus *bus = NULL;
0302         struct nvkm_i2c_aux *aux = NULL;
0303 
0304         nvkm_debug(&i2c->subdev, "ccb %02x: type %02x drive %02x "
0305                "sense %02x share %02x auxch %02x\n", i, ccbE.type,
0306                ccbE.drive, ccbE.sense, ccbE.share, ccbE.auxch);
0307 
0308         if (ccbE.share != DCB_I2C_UNUSED) {
0309             const int id = NVKM_I2C_PAD_HYBRID(ccbE.share);
0310             if (!(pad = nvkm_i2c_pad_find(i2c, id)))
0311                 ret = func->pad_s_new(i2c, id, &pad);
0312             else
0313                 ret = 0;
0314         } else {
0315             ret = func->pad_x_new(i2c, NVKM_I2C_PAD_CCB(i), &pad);
0316         }
0317 
0318         if (ret) {
0319             nvkm_error(&i2c->subdev, "ccb %02x pad, %d\n", i, ret);
0320             nvkm_i2c_pad_del(&pad);
0321             continue;
0322         }
0323 
0324         if (pad->func->bus_new_0 && ccbE.type == DCB_I2C_NV04_BIT) {
0325             ret = pad->func->bus_new_0(pad, NVKM_I2C_BUS_CCB(i),
0326                            ccbE.drive,
0327                            ccbE.sense, &bus);
0328         } else
0329         if (pad->func->bus_new_4 &&
0330             ( ccbE.type == DCB_I2C_NV4E_BIT ||
0331               ccbE.type == DCB_I2C_NVIO_BIT ||
0332              (ccbE.type == DCB_I2C_PMGR &&
0333               ccbE.drive != DCB_I2C_UNUSED))) {
0334             ret = pad->func->bus_new_4(pad, NVKM_I2C_BUS_CCB(i),
0335                            ccbE.drive, &bus);
0336         }
0337 
0338         if (ret) {
0339             nvkm_error(&i2c->subdev, "ccb %02x bus, %d\n", i, ret);
0340             nvkm_i2c_bus_del(&bus);
0341         }
0342 
0343         if (pad->func->aux_new_6 &&
0344             ( ccbE.type == DCB_I2C_NVIO_AUX ||
0345              (ccbE.type == DCB_I2C_PMGR &&
0346               ccbE.auxch != DCB_I2C_UNUSED))) {
0347             ret = pad->func->aux_new_6(pad, NVKM_I2C_BUS_CCB(i),
0348                            ccbE.auxch, &aux);
0349         } else {
0350             ret = 0;
0351         }
0352 
0353         if (ret) {
0354             nvkm_error(&i2c->subdev, "ccb %02x aux, %d\n", i, ret);
0355             nvkm_i2c_aux_del(&aux);
0356         }
0357 
0358         if (ccbE.type != DCB_I2C_UNUSED && !bus && !aux) {
0359             nvkm_warn(&i2c->subdev, "ccb %02x was ignored\n", i);
0360             continue;
0361         }
0362     }
0363 
0364     i = -1;
0365     while (dcb_outp_parse(bios, ++i, &ver, &hdr, &dcbE)) {
0366         const struct nvkm_i2c_drv *drv = nvkm_i2c_drv;
0367         struct nvkm_i2c_bus *bus;
0368         struct nvkm_i2c_pad *pad;
0369 
0370         /* internal outputs handled by native i2c busses (above) */
0371         if (!dcbE.location)
0372             continue;
0373 
0374         /* we need an i2c bus to talk to the external encoder */
0375         bus = nvkm_i2c_bus_find(i2c, dcbE.i2c_index);
0376         if (!bus) {
0377             nvkm_debug(&i2c->subdev, "dcb %02x no bus\n", i);
0378             continue;
0379         }
0380 
0381         /* ... and a driver for it */
0382         while (drv->pad_new) {
0383             if (drv->bios == dcbE.extdev)
0384                 break;
0385             drv++;
0386         }
0387 
0388         if (!drv->pad_new) {
0389             nvkm_debug(&i2c->subdev, "dcb %02x drv %02x unknown\n",
0390                    i, dcbE.extdev);
0391             continue;
0392         }
0393 
0394         /* find/create an instance of the driver */
0395         pad = nvkm_i2c_pad_find(i2c, NVKM_I2C_PAD_EXT(dcbE.extdev));
0396         if (!pad) {
0397             const int id = NVKM_I2C_PAD_EXT(dcbE.extdev);
0398             ret = drv->pad_new(bus, id, drv->addr, &pad);
0399             if (ret) {
0400                 nvkm_error(&i2c->subdev, "dcb %02x pad, %d\n",
0401                        i, ret);
0402                 nvkm_i2c_pad_del(&pad);
0403                 continue;
0404             }
0405         }
0406 
0407         /* create any i2c bus / aux channel required by the output */
0408         if (pad->func->aux_new_6 && dcbE.type == DCB_OUTPUT_DP) {
0409             const int id = NVKM_I2C_AUX_EXT(dcbE.extdev);
0410             struct nvkm_i2c_aux *aux = NULL;
0411             ret = pad->func->aux_new_6(pad, id, 0, &aux);
0412             if (ret) {
0413                 nvkm_error(&i2c->subdev, "dcb %02x aux, %d\n",
0414                        i, ret);
0415                 nvkm_i2c_aux_del(&aux);
0416             }
0417         } else
0418         if (pad->func->bus_new_4) {
0419             const int id = NVKM_I2C_BUS_EXT(dcbE.extdev);
0420             struct nvkm_i2c_bus *bus = NULL;
0421             ret = pad->func->bus_new_4(pad, id, 0, &bus);
0422             if (ret) {
0423                 nvkm_error(&i2c->subdev, "dcb %02x bus, %d\n",
0424                        i, ret);
0425                 nvkm_i2c_bus_del(&bus);
0426             }
0427         }
0428     }
0429 
0430     return nvkm_event_init(&nvkm_i2c_intr_func, 4, i, &i2c->event);
0431 }