fe0f256f11bfbcba3cc40a82eb04f4adb94a3f5c
[cascardo/linux.git] / drivers / gpu / drm / nouveau / core / engine / disp / piornv50.c
1 /*
2  * Copyright 2012 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/os.h>
26 #include <core/class.h>
27
28 #include <subdev/bios.h>
29 #include <subdev/bios/dcb.h>
30 #include <subdev/timer.h>
31 #include <subdev/i2c.h>
32
33 #include "nv50.h"
34
35 /******************************************************************************
36  * TMDS
37  *****************************************************************************/
38
39 static int
40 nv50_pior_tmds_ctor(struct nouveau_object *parent,
41                     struct nouveau_object *engine,
42                     struct nouveau_oclass *oclass, void *info, u32 index,
43                     struct nouveau_object **pobject)
44 {
45         struct nouveau_i2c *i2c = nouveau_i2c(parent);
46         struct nvkm_output *outp;
47         int ret;
48
49         ret = nvkm_output_create(parent, engine, oclass, info, index, &outp);
50         *pobject = nv_object(outp);
51         if (ret)
52                 return ret;
53
54         outp->edid = i2c->find_type(i2c, NV_I2C_TYPE_EXTDDC(outp->info.extdev));
55         return 0;
56 }
57
58 struct nvkm_output_impl
59 nv50_pior_tmds_impl = {
60         .base.handle = DCB_OUTPUT_TMDS | 0x0100,
61         .base.ofuncs = &(struct nouveau_ofuncs) {
62                 .ctor = nv50_pior_tmds_ctor,
63                 .dtor = _nvkm_output_dtor,
64                 .init = _nvkm_output_init,
65                 .fini = _nvkm_output_fini,
66         },
67 };
68
69 /******************************************************************************
70  * DisplayPort
71  *****************************************************************************/
72
73 static int
74 nv50_pior_dp_pattern(struct nvkm_output_dp *outp, int pattern)
75 {
76         struct nouveau_i2c_port *port = outp->base.edid;
77         if (port && port->func->pattern)
78                 return port->func->pattern(port, pattern);
79         return port ? 0 : -ENODEV;
80 }
81
82 static int
83 nv50_pior_dp_lnk_pwr(struct nvkm_output_dp *outp, int nr)
84 {
85         return 0;
86 }
87
88 static int
89 nv50_pior_dp_lnk_ctl(struct nvkm_output_dp *outp, int nr, int bw, bool ef)
90 {
91         struct nouveau_i2c_port *port = outp->base.edid;
92         if (port && port->func->lnk_ctl)
93                 return port->func->lnk_ctl(port, nr, bw, ef);
94         return port ? 0 : -ENODEV;
95 }
96
97 static int
98 nv50_pior_dp_drv_ctl(struct nvkm_output_dp *outp, int ln, int vs, int pe, int pc)
99 {
100         struct nouveau_i2c_port *port = outp->base.edid;
101         if (port && port->func->drv_ctl)
102                 return port->func->drv_ctl(port, ln, vs, pe);
103         return port ? 0 : -ENODEV;
104 }
105
106 static int
107 nv50_pior_dp_ctor(struct nouveau_object *parent,
108                   struct nouveau_object *engine,
109                   struct nouveau_oclass *oclass, void *info, u32 index,
110                   struct nouveau_object **pobject)
111 {
112         struct nouveau_i2c *i2c = nouveau_i2c(parent);
113         struct nvkm_output_dp *outp;
114         int ret;
115
116         ret = nvkm_output_dp_create(parent, engine, oclass, info, index, &outp);
117         *pobject = nv_object(outp);
118         if (ret)
119                 return ret;
120
121         outp->base.edid = i2c->find_type(i2c, NV_I2C_TYPE_EXTAUX(
122                                          outp->base.info.extdev));
123         return 0;
124 }
125
126 struct nvkm_output_dp_impl
127 nv50_pior_dp_impl = {
128         .base.base.handle = DCB_OUTPUT_DP | 0x0010,
129         .base.base.ofuncs = &(struct nouveau_ofuncs) {
130                 .ctor = nv50_pior_dp_ctor,
131                 .dtor = _nvkm_output_dp_dtor,
132                 .init = _nvkm_output_dp_init,
133                 .fini = _nvkm_output_dp_fini,
134         },
135         .pattern = nv50_pior_dp_pattern,
136         .lnk_pwr = nv50_pior_dp_lnk_pwr,
137         .lnk_ctl = nv50_pior_dp_lnk_ctl,
138         .drv_ctl = nv50_pior_dp_drv_ctl,
139 };
140
141 /******************************************************************************
142  * General PIOR handling
143  *****************************************************************************/
144
145 int
146 nv50_pior_power(struct nv50_disp_priv *priv, int or, u32 data)
147 {
148         const u32 stat = data & NV50_DISP_PIOR_PWR_STATE;
149         const u32 soff = (or * 0x800);
150         nv_wait(priv, 0x61e004 + soff, 0x80000000, 0x00000000);
151         nv_mask(priv, 0x61e004 + soff, 0x80000101, 0x80000000 | stat);
152         nv_wait(priv, 0x61e004 + soff, 0x80000000, 0x00000000);
153         return 0;
154 }
155
156 int
157 nv50_pior_mthd(struct nouveau_object *object, u32 mthd, void *args, u32 size)
158 {
159         struct nv50_disp_priv *priv = (void *)object->engine;
160         const u8 type = (mthd & NV50_DISP_PIOR_MTHD_TYPE) >> 12;
161         const u8 or   = (mthd & NV50_DISP_PIOR_MTHD_OR);
162         u32 *data = args;
163         int ret;
164
165         if (size < sizeof(u32))
166                 return -EINVAL;
167
168         mthd &= ~NV50_DISP_PIOR_MTHD_TYPE;
169         mthd &= ~NV50_DISP_PIOR_MTHD_OR;
170         switch (mthd) {
171         case NV50_DISP_PIOR_PWR:
172                 ret = priv->pior.power(priv, or, data[0]);
173                 priv->pior.type[or] = type;
174                 break;
175         default:
176                 return -EINVAL;
177         }
178
179         return ret;
180 }