drm/nouveau/clock: pull in the implementation from all over the place
[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/pll.h>
6 #include <subdev/gpio.h>
7 #include <subdev/i2c.h>
8 #include <subdev/clock.h>
9
10 void *nouveau_newpriv(struct drm_device *);
11
12 u8
13 _nv_rd08(struct drm_device *dev, u32 reg)
14 {
15         struct nouveau_drm *drm = nouveau_newpriv(dev);
16         return nv_ro08(drm->device, reg);
17 }
18
19 void
20 _nv_wr08(struct drm_device *dev, u32 reg, u8 val)
21 {
22         struct nouveau_drm *drm = nouveau_newpriv(dev);
23         nv_wo08(drm->device, reg, val);
24 }
25
26 u32
27 _nv_rd32(struct drm_device *dev, u32 reg)
28 {
29         struct nouveau_drm *drm = nouveau_newpriv(dev);
30         return nv_ro32(drm->device, reg);
31 }
32
33 void
34 _nv_wr32(struct drm_device *dev, u32 reg, u32 val)
35 {
36         struct nouveau_drm *drm = nouveau_newpriv(dev);
37         nv_wo32(drm->device, reg, val);
38 }
39
40 u32
41 _nv_mask(struct drm_device *dev, u32 reg, u32 mask, u32 val)
42 {
43         u32 tmp = _nv_rd32(dev, reg);
44         _nv_wr32(dev, reg, (tmp & ~mask) | val);
45         return tmp;
46 }
47
48 bool
49 _nv_bios(struct drm_device *dev, u8 **data, u32 *size)
50 {
51         struct nouveau_drm *drm = nouveau_newpriv(dev);
52         struct nouveau_bios *bios = nouveau_bios(drm->device);
53         *data = bios->data;
54         *size = bios->size;
55         return true;
56 }
57
58 void
59 nouveau_gpio_reset(struct drm_device *dev)
60 {
61         struct nouveau_drm *drm = nouveau_newpriv(dev);
62         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
63         gpio->reset(gpio);
64 }
65
66 int
67 nouveau_gpio_find(struct drm_device *dev, int idx, u8 tag, u8 line,
68                   struct dcb_gpio_func *func)
69 {
70         struct nouveau_drm *drm = nouveau_newpriv(dev);
71         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
72
73         return gpio->find(gpio, idx, tag, line, func);
74 }
75
76 bool
77 nouveau_gpio_func_valid(struct drm_device *dev, u8 tag)
78 {
79         struct nouveau_drm *drm = nouveau_newpriv(dev);
80         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
81         struct dcb_gpio_func func;
82
83         return gpio->find(gpio, 0, tag, 0xff, &func) == 0;
84 }
85
86 int
87 nouveau_gpio_func_set(struct drm_device *dev, u8 tag, int state)
88 {
89         struct nouveau_drm *drm = nouveau_newpriv(dev);
90         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
91         if (gpio && gpio->get)
92                 return gpio->set(gpio, 0, tag, 0xff, state);
93         return -ENODEV;
94 }
95
96 int
97 nouveau_gpio_func_get(struct drm_device *dev, u8 tag)
98 {
99         struct nouveau_drm *drm = nouveau_newpriv(dev);
100         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
101         if (gpio && gpio->get)
102                 return gpio->get(gpio, 0, tag, 0xff);
103         return -ENODEV;
104 }
105
106 int
107 nouveau_gpio_irq(struct drm_device *dev, int idx, u8 tag, u8 line, bool on)
108 {
109         struct nouveau_drm *drm = nouveau_newpriv(dev);
110         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
111         if (gpio && gpio->irq)
112                 return gpio->irq(gpio, idx, tag, line, on);
113         return -ENODEV;
114 }
115
116 int
117 nouveau_gpio_isr_add(struct drm_device *dev, int idx, u8 tag, u8 line,
118                      void (*exec)(void *, int state), void *data)
119 {
120         struct nouveau_drm *drm = nouveau_newpriv(dev);
121         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
122         if (gpio && gpio->isr_add)
123                 return gpio->isr_add(gpio, idx, tag, line, exec, data);
124         return -ENODEV;
125 }
126
127 void
128 nouveau_gpio_isr_del(struct drm_device *dev, int idx, u8 tag, u8 line,
129                      void (*exec)(void *, int state), void *data)
130 {
131         struct nouveau_drm *drm = nouveau_newpriv(dev);
132         struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
133         if (gpio && gpio->isr_del)
134                 gpio->isr_del(gpio, idx, tag, line, exec, data);
135 }
136
137 struct nouveau_i2c_port *
138 nouveau_i2c_find(struct drm_device *dev, u8 index)
139 {
140         struct nouveau_drm *drm = nouveau_newpriv(dev);
141         struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
142
143         return i2c->find(i2c, index);
144 }
145
146 bool
147 nouveau_probe_i2c_addr(struct nouveau_i2c_port *port, int addr)
148 {
149         return nv_probe_i2c(port, addr);
150 }
151
152 struct i2c_adapter *
153 nouveau_i2c_adapter(struct nouveau_i2c_port *port)
154 {
155         return &port->adapter;
156 }
157
158
159 int
160 nouveau_i2c_identify(struct drm_device *dev, const char *what,
161                      struct i2c_board_info *info,
162                      bool (*match)(struct nouveau_i2c_port *,
163                                    struct i2c_board_info *),
164                      int index)
165 {
166         struct nouveau_drm *drm = nouveau_newpriv(dev);
167         struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
168
169         return i2c->identify(i2c, index, what, info, match);
170 }
171
172 int
173 auxch_rd(struct drm_device *dev, struct nouveau_i2c_port *port,
174          u32 addr, u8 *data, u8 size)
175 {
176         return nv_rdaux(port, addr, data, size);
177 }
178
179 int
180 auxch_wr(struct drm_device *dev, struct nouveau_i2c_port *port,
181          u32 addr, u8 *data, u8 size)
182 {
183         return nv_wraux(port, addr, data, size);
184 }
185
186 u32
187 get_pll_register(struct drm_device *dev, u32 type)
188 {
189         struct nouveau_drm *drm = nouveau_newpriv(dev);
190         struct nouveau_bios *bios = nouveau_bios(drm->device);
191         struct nvbios_pll info;
192
193         if (nvbios_pll_parse(bios, type, &info))
194                 return 0;
195         return info.reg;
196 }
197
198 int
199 get_pll_limits(struct drm_device *dev, u32 type, struct nvbios_pll *info)
200 {
201         struct nouveau_drm *drm = nouveau_newpriv(dev);
202         struct nouveau_bios *bios = nouveau_bios(drm->device);
203
204         return nvbios_pll_parse(bios, type, info);
205 }
206
207 int
208 setPLL(struct drm_device *dev, u32 reg, u32 freq)
209 {
210         struct nouveau_drm *drm = nouveau_newpriv(dev);
211         struct nouveau_clock *clk = nouveau_clock(drm->device);
212         int ret = -ENODEV;
213
214         if (clk->pll_set)
215                 ret = clk->pll_set(clk, reg, freq);
216         return ret;
217 }
218
219
220 int
221 nouveau_calc_pll_mnp(struct drm_device *dev, struct nvbios_pll *info,
222                      int freq, struct nouveau_pll_vals *pv)
223 {
224         struct nouveau_drm *drm = nouveau_newpriv(dev);
225         struct nouveau_clock *clk = nouveau_clock(drm->device);
226         int ret = 0;
227
228         if (clk->pll_calc)
229                 ret = clk->pll_calc(clk, info, freq, pv);
230         return ret;
231 }
232
233 int
234 nouveau_hw_setpll(struct drm_device *dev, u32 reg1,
235                   struct nouveau_pll_vals *pv)
236 {
237         struct nouveau_drm *drm = nouveau_newpriv(dev);
238         struct nouveau_clock *clk = nouveau_clock(drm->device);
239         int ret = -ENODEV;
240
241         if (clk->pll_prog)
242                 ret = clk->pll_prog(clk, reg1, pv);
243         return ret;
244 }
245
246 int nva3_pll_calc(struct nouveau_clock *, struct nvbios_pll *, u32 freq,
247                   int *N, int *fN, int *M, int *P);
248
249 int
250 nva3_calc_pll(struct drm_device *dev, struct nvbios_pll *info, u32 freq,
251               int *N, int *fN, int *M, int *P)
252 {
253         struct nouveau_drm *drm = nouveau_newpriv(dev);
254         struct nouveau_clock *clk = nouveau_clock(drm->device);
255
256         return nva3_pll_calc(clk, info, freq, N, fN, M, P);
257 }