drm/nouveau/mc: port to subdev interfaces
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / nouveau_compat.c
1 #include "nouveau_drm.h"
2 #include "nouveau_compat.h"
3
4 #include <subdev/bios.h>
5 #include <subdev/bios/dcb.h>
6 #include <subdev/bios/init.h>
7 #include <subdev/bios/pll.h>
8 #include <subdev/gpio.h>
9 #include <subdev/i2c.h>
10 #include <subdev/clock.h>
11 #include <subdev/mc.h>
12
13 void *nouveau_newpriv(struct drm_device *);
14
15 int
16 nvdrm_gart_init(struct drm_device *dev, u64 *base, u64 *size)
17 {
18         struct nouveau_drm *drm = nouveau_newpriv(dev);
19         if (drm->agp.stat == ENABLED) {
20                 *base = drm->agp.base;
21                 *size = drm->agp.base;
22                 return 0;
23         }
24         return -ENODEV;
25 }
26
27 u8
28 _nv_rd08(struct drm_device *dev, u32 reg)
29 {
30         struct nouveau_drm *drm = nouveau_newpriv(dev);
31         return nv_ro08(drm->device, reg);
32 }
33
34 void
35 _nv_wr08(struct drm_device *dev, u32 reg, u8 val)
36 {
37         struct nouveau_drm *drm = nouveau_newpriv(dev);
38         nv_wo08(drm->device, reg, val);
39 }
40
41 u32
42 _nv_rd32(struct drm_device *dev, u32 reg)
43 {
44         struct nouveau_drm *drm = nouveau_newpriv(dev);
45         return nv_ro32(drm->device, reg);
46 }
47
48 void
49 _nv_wr32(struct drm_device *dev, u32 reg, u32 val)
50 {
51         struct nouveau_drm *drm = nouveau_newpriv(dev);
52         nv_wo32(drm->device, reg, val);
53 }
54
55 u32
56 _nv_mask(struct drm_device *dev, u32 reg, u32 mask, u32 val)
57 {
58         u32 tmp = _nv_rd32(dev, reg);
59         _nv_wr32(dev, reg, (tmp & ~mask) | val);
60         return tmp;
61 }
62
63 bool
64 _nv_bios(struct drm_device *dev, u8 **data, u32 *size)
65 {
66         struct nouveau_drm *drm = nouveau_newpriv(dev);
67         struct nouveau_bios *bios = nouveau_bios(drm->device);
68         *data = bios->data;
69         *size = bios->size;
70         return true;
71 }
72
73 void
74 nouveau_gpio_reset(struct drm_device *dev)
75 {
76         struct nouveau_drm *drm = nouveau_newpriv(dev);
77         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
78         gpio->reset(gpio);
79 }
80
81 int
82 nouveau_gpio_find(struct drm_device *dev, int idx, u8 tag, u8 line,
83                   struct dcb_gpio_func *func)
84 {
85         struct nouveau_drm *drm = nouveau_newpriv(dev);
86         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
87
88         return gpio->find(gpio, idx, tag, line, func);
89 }
90
91 bool
92 nouveau_gpio_func_valid(struct drm_device *dev, u8 tag)
93 {
94         struct nouveau_drm *drm = nouveau_newpriv(dev);
95         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
96         struct dcb_gpio_func func;
97
98         return gpio->find(gpio, 0, tag, 0xff, &func) == 0;
99 }
100
101 int
102 nouveau_gpio_func_set(struct drm_device *dev, u8 tag, int state)
103 {
104         struct nouveau_drm *drm = nouveau_newpriv(dev);
105         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
106         if (gpio && gpio->get)
107                 return gpio->set(gpio, 0, tag, 0xff, state);
108         return -ENODEV;
109 }
110
111 int
112 nouveau_gpio_func_get(struct drm_device *dev, u8 tag)
113 {
114         struct nouveau_drm *drm = nouveau_newpriv(dev);
115         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
116         if (gpio && gpio->get)
117                 return gpio->get(gpio, 0, tag, 0xff);
118         return -ENODEV;
119 }
120
121 int
122 nouveau_gpio_irq(struct drm_device *dev, int idx, u8 tag, u8 line, bool on)
123 {
124         struct nouveau_drm *drm = nouveau_newpriv(dev);
125         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
126         if (gpio && gpio->irq)
127                 return gpio->irq(gpio, idx, tag, line, on);
128         return -ENODEV;
129 }
130
131 int
132 nouveau_gpio_isr_add(struct drm_device *dev, int idx, u8 tag, u8 line,
133                      void (*exec)(void *, int state), void *data)
134 {
135         struct nouveau_drm *drm = nouveau_newpriv(dev);
136         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
137         if (gpio && gpio->isr_add)
138                 return gpio->isr_add(gpio, idx, tag, line, exec, data);
139         return -ENODEV;
140 }
141
142 void
143 nouveau_gpio_isr_del(struct drm_device *dev, int idx, u8 tag, u8 line,
144                      void (*exec)(void *, int state), void *data)
145 {
146         struct nouveau_drm *drm = nouveau_newpriv(dev);
147         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
148         if (gpio && gpio->isr_del)
149                 gpio->isr_del(gpio, idx, tag, line, exec, data);
150 }
151
152 struct nouveau_i2c_port *
153 nouveau_i2c_find(struct drm_device *dev, u8 index)
154 {
155         struct nouveau_drm *drm = nouveau_newpriv(dev);
156         struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
157
158         return i2c->find(i2c, index);
159 }
160
161 bool
162 nouveau_probe_i2c_addr(struct nouveau_i2c_port *port, int addr)
163 {
164         return nv_probe_i2c(port, addr);
165 }
166
167 struct i2c_adapter *
168 nouveau_i2c_adapter(struct nouveau_i2c_port *port)
169 {
170         return &port->adapter;
171 }
172
173
174 int
175 nouveau_i2c_identify(struct drm_device *dev, const char *what,
176                      struct i2c_board_info *info,
177                      bool (*match)(struct nouveau_i2c_port *,
178                                    struct i2c_board_info *),
179                      int index)
180 {
181         struct nouveau_drm *drm = nouveau_newpriv(dev);
182         struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
183
184         return i2c->identify(i2c, index, what, info, match);
185 }
186
187 int
188 auxch_rd(struct drm_device *dev, struct nouveau_i2c_port *port,
189          u32 addr, u8 *data, u8 size)
190 {
191         return nv_rdaux(port, addr, data, size);
192 }
193
194 int
195 auxch_wr(struct drm_device *dev, struct nouveau_i2c_port *port,
196          u32 addr, u8 *data, u8 size)
197 {
198         return nv_wraux(port, addr, data, size);
199 }
200
201 u32
202 get_pll_register(struct drm_device *dev, u32 type)
203 {
204         struct nouveau_drm *drm = nouveau_newpriv(dev);
205         struct nouveau_bios *bios = nouveau_bios(drm->device);
206         struct nvbios_pll info;
207
208         if (nvbios_pll_parse(bios, type, &info))
209                 return 0;
210         return info.reg;
211 }
212
213 int
214 get_pll_limits(struct drm_device *dev, u32 type, struct nvbios_pll *info)
215 {
216         struct nouveau_drm *drm = nouveau_newpriv(dev);
217         struct nouveau_bios *bios = nouveau_bios(drm->device);
218
219         return nvbios_pll_parse(bios, type, info);
220 }
221
222 int
223 setPLL(struct drm_device *dev, u32 reg, u32 freq)
224 {
225         struct nouveau_drm *drm = nouveau_newpriv(dev);
226         struct nouveau_clock *clk = nouveau_clock(drm->device);
227         int ret = -ENODEV;
228
229         if (clk->pll_set)
230                 ret = clk->pll_set(clk, reg, freq);
231         return ret;
232 }
233
234
235 int
236 nouveau_calc_pll_mnp(struct drm_device *dev, struct nvbios_pll *info,
237                      int freq, struct nouveau_pll_vals *pv)
238 {
239         struct nouveau_drm *drm = nouveau_newpriv(dev);
240         struct nouveau_clock *clk = nouveau_clock(drm->device);
241         int ret = 0;
242
243         if (clk->pll_calc)
244                 ret = clk->pll_calc(clk, info, freq, pv);
245         return ret;
246 }
247
248 int
249 nouveau_hw_setpll(struct drm_device *dev, u32 reg1,
250                   struct nouveau_pll_vals *pv)
251 {
252         struct nouveau_drm *drm = nouveau_newpriv(dev);
253         struct nouveau_clock *clk = nouveau_clock(drm->device);
254         int ret = -ENODEV;
255
256         if (clk->pll_prog)
257                 ret = clk->pll_prog(clk, reg1, pv);
258         return ret;
259 }
260
261 int nva3_pll_calc(struct nouveau_clock *, struct nvbios_pll *, u32 freq,
262                   int *N, int *fN, int *M, int *P);
263
264 int
265 nva3_calc_pll(struct drm_device *dev, struct nvbios_pll *info, u32 freq,
266               int *N, int *fN, int *M, int *P)
267 {
268         struct nouveau_drm *drm = nouveau_newpriv(dev);
269         struct nouveau_clock *clk = nouveau_clock(drm->device);
270
271         return nva3_pll_calc(clk, info, freq, N, fN, M, P);
272 }
273
274 void
275 nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,
276                             struct dcb_output *dcbent, int crtc)
277 {
278         struct nouveau_drm *drm = nouveau_newpriv(dev);
279         struct nouveau_bios *bios = nouveau_bios(drm->device);
280         struct nvbios_init init = {
281                 .subdev = nv_subdev(bios),
282                 .bios = bios,
283                 .offset = table,
284                 .outp = dcbent,
285                 .crtc = crtc,
286                 .execute = 1
287         };
288
289         nvbios_exec(&init);
290 }
291
292 void
293 nouveau_bios_init_exec(struct drm_device *dev, uint16_t table)
294 {
295         nouveau_bios_run_init_table(dev, table, NULL, 0);
296 }
297
298 void
299 nv_intr(struct drm_device *dev)
300 {
301         struct nouveau_drm *drm = nouveau_newpriv(dev);
302         struct nouveau_mc *pmc = nouveau_mc(drm->device);
303         nv_subdev(pmc)->intr(pmc);
304 }