drm/nouveau: port remainder of drm code, and rip out compat layer
[cascardo/linux.git] / drivers / gpu / drm / nouveau / core / subdev / bios / gpio.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 <subdev/bios.h>
26 #include <subdev/bios/dcb.h>
27 #include <subdev/bios/gpio.h>
28
29 u16
30 dcb_gpio_table(struct nouveau_bios *bios)
31 {
32         u8  ver, hdr, cnt, len;
33         u16 dcb = dcb_table(bios, &ver, &hdr, &cnt, &len);
34         if (dcb) {
35                 if (ver >= 0x30 && hdr >= 0x0c)
36                         return nv_ro16(bios, dcb + 0x0a);
37                 if (ver >= 0x22 && nv_ro08(bios, dcb - 1) >= 0x13)
38                         return nv_ro16(bios, dcb - 0x0f);
39         }
40         return 0x0000;
41 }
42
43 u16
44 dcb_gpio_entry(struct nouveau_bios *bios, int idx, int ent, u8 *ver)
45 {
46         u16 gpio = dcb_gpio_table(bios);
47         if (gpio) {
48                 *ver = nv_ro08(bios, gpio);
49                 if (*ver < 0x30 && ent < nv_ro08(bios, gpio + 2))
50                         return gpio + 3 + (ent * nv_ro08(bios, gpio + 1));
51                 else if (ent < nv_ro08(bios, gpio + 2))
52                         return gpio + nv_ro08(bios, gpio + 1) +
53                                (ent * nv_ro08(bios, gpio + 3));
54         }
55         return 0x0000;
56 }
57
58 int
59 dcb_gpio_parse(struct nouveau_bios *bios, int idx, u8 func, u8 line,
60                struct dcb_gpio_func *gpio)
61 {
62         u8  ver, hdr, cnt, len;
63         u16 entry;
64         int i = -1;
65
66         while ((entry = dcb_gpio_entry(bios, idx, ++i, &ver))) {
67                 if (ver < 0x40) {
68                         u16 data = nv_ro16(bios, entry);
69                         *gpio = (struct dcb_gpio_func) {
70                                 .line = (data & 0x001f) >> 0,
71                                 .func = (data & 0x07e0) >> 5,
72                                 .log[0] = (data & 0x1800) >> 11,
73                                 .log[1] = (data & 0x6000) >> 13,
74                         };
75                 } else
76                 if (ver < 0x41) {
77                         u32 data = nv_ro32(bios, entry);
78                         *gpio = (struct dcb_gpio_func) {
79                                 .line = (data & 0x0000001f) >> 0,
80                                 .func = (data & 0x0000ff00) >> 8,
81                                 .log[0] = (data & 0x18000000) >> 27,
82                                 .log[1] = (data & 0x60000000) >> 29,
83                         };
84                 } else {
85                         u32 data = nv_ro32(bios, entry + 0);
86                         u8 data1 = nv_ro32(bios, entry + 4);
87                         *gpio = (struct dcb_gpio_func) {
88                                 .line = (data & 0x0000003f) >> 0,
89                                 .func = (data & 0x0000ff00) >> 8,
90                                 .log[0] = (data1 & 0x30) >> 4,
91                                 .log[1] = (data1 & 0xc0) >> 6,
92                         };
93                 }
94
95                 if ((line == 0xff || line == gpio->line) &&
96                     (func == 0xff || func == gpio->func))
97                         return 0;
98         }
99
100         /* DCB 2.2, fixed TVDAC GPIO data */
101         if ((entry = dcb_table(bios, &ver, &hdr, &cnt, &len)) && ver >= 0x22) {
102                 if (func == DCB_GPIO_TVDAC0) {
103                         *gpio = (struct dcb_gpio_func) {
104                                 .func = DCB_GPIO_TVDAC0,
105                                 .line = nv_ro08(bios, entry - 4) >> 4,
106                                 .log[0] = !!(nv_ro08(bios, entry - 5) & 2),
107                                 .log[1] =  !(nv_ro08(bios, entry - 5) & 2),
108                         };
109                         return 0;
110                 }
111         }
112
113         return -EINVAL;
114 }