]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/nouveau/core/subdev/gpio/nv50.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6
[karo-tx-linux.git] / drivers / gpu / drm / nouveau / core / subdev / gpio / nv50.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 "priv.h"
26
27 struct nv50_gpio_priv {
28         struct nouveau_gpio base;
29 };
30
31 static void
32 nv50_gpio_reset(struct nouveau_gpio *gpio, u8 match)
33 {
34         struct nouveau_bios *bios = nouveau_bios(gpio);
35         struct nv50_gpio_priv *priv = (void *)gpio;
36         u8 ver, len;
37         u16 entry;
38         int ent = -1;
39
40         while ((entry = dcb_gpio_entry(bios, 0, ++ent, &ver, &len))) {
41                 static const u32 regs[] = { 0xe100, 0xe28c };
42                 u32 data = nv_ro32(bios, entry);
43                 u8  line =   (data & 0x0000001f);
44                 u8  func =   (data & 0x0000ff00) >> 8;
45                 u8  defs = !!(data & 0x01000000);
46                 u8  unk0 = !!(data & 0x02000000);
47                 u8  unk1 = !!(data & 0x04000000);
48                 u32 val = (unk1 << 16) | unk0;
49                 u32 reg = regs[line >> 4];
50                 u32 lsh = line & 0x0f;
51
52                 if ( func  == DCB_GPIO_UNUSED ||
53                     (match != DCB_GPIO_UNUSED && match != func))
54                         continue;
55
56                 gpio->set(gpio, 0, func, line, defs);
57
58                 nv_mask(priv, reg, 0x00010001 << lsh, val << lsh);
59         }
60 }
61
62 static int
63 nv50_gpio_location(int line, u32 *reg, u32 *shift)
64 {
65         const u32 nv50_gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
66
67         if (line >= 32)
68                 return -EINVAL;
69
70         *reg = nv50_gpio_reg[line >> 3];
71         *shift = (line & 7) << 2;
72         return 0;
73 }
74
75 static int
76 nv50_gpio_drive(struct nouveau_gpio *gpio, int line, int dir, int out)
77 {
78         u32 reg, shift;
79
80         if (nv50_gpio_location(line, &reg, &shift))
81                 return -EINVAL;
82
83         nv_mask(gpio, reg, 3 << shift, (((dir ^ 1) << 1) | out) << shift);
84         return 0;
85 }
86
87 static int
88 nv50_gpio_sense(struct nouveau_gpio *gpio, int line)
89 {
90         u32 reg, shift;
91
92         if (nv50_gpio_location(line, &reg, &shift))
93                 return -EINVAL;
94
95         return !!(nv_rd32(gpio, reg) & (4 << shift));
96 }
97
98 void
99 nv50_gpio_intr(struct nouveau_subdev *subdev)
100 {
101         struct nv50_gpio_priv *priv = (void *)subdev;
102         u32 intr0, intr1 = 0;
103         u32 hi, lo;
104         int i;
105
106         intr0 = nv_rd32(priv, 0xe054) & nv_rd32(priv, 0xe050);
107         if (nv_device(priv)->chipset > 0x92)
108                 intr1 = nv_rd32(priv, 0xe074) & nv_rd32(priv, 0xe070);
109
110         hi = (intr0 & 0x0000ffff) | (intr1 << 16);
111         lo = (intr0 >> 16) | (intr1 & 0xffff0000);
112
113         for (i = 0; (hi | lo) && i < 32; i++) {
114                 if ((hi | lo) & (1 << i))
115                         nouveau_event_trigger(priv->base.events, i);
116         }
117
118         nv_wr32(priv, 0xe054, intr0);
119         if (nv_device(priv)->chipset > 0x92)
120                 nv_wr32(priv, 0xe074, intr1);
121 }
122
123 void
124 nv50_gpio_intr_enable(struct nouveau_event *event, int line)
125 {
126         const u32 addr = line < 16 ? 0xe050 : 0xe070;
127         const u32 mask = 0x00010001 << (line & 0xf);
128         nv_wr32(event->priv, addr + 0x04, mask);
129         nv_mask(event->priv, addr + 0x00, mask, mask);
130 }
131
132 void
133 nv50_gpio_intr_disable(struct nouveau_event *event, int line)
134 {
135         const u32 addr = line < 16 ? 0xe050 : 0xe070;
136         const u32 mask = 0x00010001 << (line & 0xf);
137         nv_wr32(event->priv, addr + 0x04, mask);
138         nv_mask(event->priv, addr + 0x00, mask, 0x00000000);
139 }
140
141 static int
142 nv50_gpio_ctor(struct nouveau_object *parent, struct nouveau_object *engine,
143                struct nouveau_oclass *oclass, void *data, u32 size,
144                struct nouveau_object **pobject)
145 {
146         struct nv50_gpio_priv *priv;
147         int ret;
148
149         ret = nouveau_gpio_create(parent, engine, oclass,
150                                   nv_device(parent)->chipset > 0x92 ? 32 : 16,
151                                   &priv);
152         *pobject = nv_object(priv);
153         if (ret)
154                 return ret;
155
156         priv->base.reset = nv50_gpio_reset;
157         priv->base.drive = nv50_gpio_drive;
158         priv->base.sense = nv50_gpio_sense;
159         priv->base.events->priv = priv;
160         priv->base.events->enable = nv50_gpio_intr_enable;
161         priv->base.events->disable = nv50_gpio_intr_disable;
162         nv_subdev(priv)->intr = nv50_gpio_intr;
163         return 0;
164 }
165
166 void
167 nv50_gpio_dtor(struct nouveau_object *object)
168 {
169         struct nv50_gpio_priv *priv = (void *)object;
170         nouveau_gpio_destroy(&priv->base);
171 }
172
173 int
174 nv50_gpio_init(struct nouveau_object *object)
175 {
176         struct nv50_gpio_priv *priv = (void *)object;
177         int ret;
178
179         ret = nouveau_gpio_init(&priv->base);
180         if (ret)
181                 return ret;
182
183         /* disable, and ack any pending gpio interrupts */
184         nv_wr32(priv, 0xe050, 0x00000000);
185         nv_wr32(priv, 0xe054, 0xffffffff);
186         if (nv_device(priv)->chipset > 0x92) {
187                 nv_wr32(priv, 0xe070, 0x00000000);
188                 nv_wr32(priv, 0xe074, 0xffffffff);
189         }
190
191         return 0;
192 }
193
194 int
195 nv50_gpio_fini(struct nouveau_object *object, bool suspend)
196 {
197         struct nv50_gpio_priv *priv = (void *)object;
198         nv_wr32(priv, 0xe050, 0x00000000);
199         if (nv_device(priv)->chipset > 0x92)
200                 nv_wr32(priv, 0xe070, 0x00000000);
201         return nouveau_gpio_fini(&priv->base, suspend);
202 }
203
204 struct nouveau_oclass
205 nv50_gpio_oclass = {
206         .handle = NV_SUBDEV(GPIO, 0x50),
207         .ofuncs = &(struct nouveau_ofuncs) {
208                 .ctor = nv50_gpio_ctor,
209                 .dtor = nv50_gpio_dtor,
210                 .init = nv50_gpio_init,
211                 .fini = nv50_gpio_fini,
212         },
213 };