]> Pileus Git - ~andy/linux/blob - drivers/gpu/drm/armada/armada_drv.c
DRM: armada: fix use of kfifo_put()
[~andy/linux] / drivers / gpu / drm / armada / armada_drv.c
1 /*
2  * Copyright (C) 2012 Russell King
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #include <linux/clk.h>
9 #include <linux/module.h>
10 #include <drm/drmP.h>
11 #include <drm/drm_crtc_helper.h>
12 #include "armada_crtc.h"
13 #include "armada_drm.h"
14 #include "armada_gem.h"
15 #include "armada_hw.h"
16 #include <drm/armada_drm.h>
17 #include "armada_ioctlP.h"
18
19 #ifdef CONFIG_DRM_ARMADA_TDA1998X
20 #include <drm/i2c/tda998x.h>
21 #include "armada_slave.h"
22
23 static struct tda998x_encoder_params params = {
24         /* With 0x24, there is no translation between vp_out and int_vp
25         FB      LCD out Pins    VIP     Int Vp
26         R:23:16 R:7:0   VPC7:0  7:0     7:0[R]
27         G:15:8  G:15:8  VPB7:0  23:16   23:16[G]
28         B:7:0   B:23:16 VPA7:0  15:8    15:8[B]
29         */
30         .swap_a = 2,
31         .swap_b = 3,
32         .swap_c = 4,
33         .swap_d = 5,
34         .swap_e = 0,
35         .swap_f = 1,
36         .audio_cfg = BIT(2),
37         .audio_frame[1] = 1,
38         .audio_format = AFMT_SPDIF,
39         .audio_sample_rate = 44100,
40 };
41
42 static const struct armada_drm_slave_config tda19988_config = {
43         .i2c_adapter_id = 0,
44         .crtcs = 1 << 0, /* Only LCD0 at the moment */
45         .polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT,
46         .interlace_allowed = true,
47         .info = {
48                 .type = "tda998x",
49                 .addr = 0x70,
50                 .platform_data = &params,
51         },
52 };
53 #endif
54
55 static void armada_drm_unref_work(struct work_struct *work)
56 {
57         struct armada_private *priv =
58                 container_of(work, struct armada_private, fb_unref_work);
59         struct drm_framebuffer *fb;
60
61         while (kfifo_get(&priv->fb_unref, &fb))
62                 drm_framebuffer_unreference(fb);
63 }
64
65 /* Must be called with dev->event_lock held */
66 void __armada_drm_queue_unref_work(struct drm_device *dev,
67         struct drm_framebuffer *fb)
68 {
69         struct armada_private *priv = dev->dev_private;
70
71         WARN_ON(!kfifo_put(&priv->fb_unref, fb));
72         schedule_work(&priv->fb_unref_work);
73 }
74
75 void armada_drm_queue_unref_work(struct drm_device *dev,
76         struct drm_framebuffer *fb)
77 {
78         unsigned long flags;
79
80         spin_lock_irqsave(&dev->event_lock, flags);
81         __armada_drm_queue_unref_work(dev, fb);
82         spin_unlock_irqrestore(&dev->event_lock, flags);
83 }
84
85 static int armada_drm_load(struct drm_device *dev, unsigned long flags)
86 {
87         const struct platform_device_id *id;
88         struct armada_private *priv;
89         struct resource *res[ARRAY_SIZE(priv->dcrtc)];
90         struct resource *mem = NULL;
91         int ret, n, i;
92
93         memset(res, 0, sizeof(res));
94
95         for (n = i = 0; ; n++) {
96                 struct resource *r = platform_get_resource(dev->platformdev,
97                                                            IORESOURCE_MEM, n);
98                 if (!r)
99                         break;
100
101                 /* Resources above 64K are graphics memory */
102                 if (resource_size(r) > SZ_64K)
103                         mem = r;
104                 else if (i < ARRAY_SIZE(priv->dcrtc))
105                         res[i++] = r;
106                 else
107                         return -EINVAL;
108         }
109
110         if (!res[0] || !mem)
111                 return -ENXIO;
112
113         if (!devm_request_mem_region(dev->dev, mem->start,
114                         resource_size(mem), "armada-drm"))
115                 return -EBUSY;
116
117         priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
118         if (!priv) {
119                 DRM_ERROR("failed to allocate private\n");
120                 return -ENOMEM;
121         }
122
123         dev->dev_private = priv;
124
125         /* Get the implementation specific driver data. */
126         id = platform_get_device_id(dev->platformdev);
127         if (!id)
128                 return -ENXIO;
129
130         priv->variant = (struct armada_variant *)id->driver_data;
131
132         ret = priv->variant->init(priv, dev->dev);
133         if (ret)
134                 return ret;
135
136         INIT_WORK(&priv->fb_unref_work, armada_drm_unref_work);
137         INIT_KFIFO(priv->fb_unref);
138
139         /* Mode setting support */
140         drm_mode_config_init(dev);
141         dev->mode_config.min_width = 320;
142         dev->mode_config.min_height = 200;
143
144         /*
145          * With vscale enabled, the maximum width is 1920 due to the
146          * 1920 by 3 lines RAM
147          */
148         dev->mode_config.max_width = 1920;
149         dev->mode_config.max_height = 2048;
150
151         dev->mode_config.preferred_depth = 24;
152         dev->mode_config.funcs = &armada_drm_mode_config_funcs;
153         drm_mm_init(&priv->linear, mem->start, resource_size(mem));
154
155         /* Create all LCD controllers */
156         for (n = 0; n < ARRAY_SIZE(priv->dcrtc); n++) {
157                 if (!res[n])
158                         break;
159
160                 ret = armada_drm_crtc_create(dev, n, res[n]);
161                 if (ret)
162                         goto err_kms;
163         }
164
165 #ifdef CONFIG_DRM_ARMADA_TDA1998X
166         ret = armada_drm_connector_slave_create(dev, &tda19988_config);
167         if (ret)
168                 goto err_kms;
169 #endif
170
171         ret = drm_vblank_init(dev, n);
172         if (ret)
173                 goto err_kms;
174
175         ret = drm_irq_install(dev);
176         if (ret)
177                 goto err_kms;
178
179         dev->vblank_disable_allowed = 1;
180
181         ret = armada_fbdev_init(dev);
182         if (ret)
183                 goto err_irq;
184
185         drm_kms_helper_poll_init(dev);
186
187         return 0;
188
189  err_irq:
190         drm_irq_uninstall(dev);
191  err_kms:
192         drm_mode_config_cleanup(dev);
193         drm_mm_takedown(&priv->linear);
194         flush_work(&priv->fb_unref_work);
195
196         return ret;
197 }
198
199 static int armada_drm_unload(struct drm_device *dev)
200 {
201         struct armada_private *priv = dev->dev_private;
202
203         drm_kms_helper_poll_fini(dev);
204         armada_fbdev_fini(dev);
205         drm_irq_uninstall(dev);
206         drm_mode_config_cleanup(dev);
207         drm_mm_takedown(&priv->linear);
208         flush_work(&priv->fb_unref_work);
209         dev->dev_private = NULL;
210
211         return 0;
212 }
213
214 void armada_drm_vbl_event_add(struct armada_crtc *dcrtc,
215         struct armada_vbl_event *evt)
216 {
217         unsigned long flags;
218
219         spin_lock_irqsave(&dcrtc->irq_lock, flags);
220         if (list_empty(&evt->node)) {
221                 list_add_tail(&evt->node, &dcrtc->vbl_list);
222
223                 drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
224         }
225         spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
226 }
227
228 void armada_drm_vbl_event_remove(struct armada_crtc *dcrtc,
229         struct armada_vbl_event *evt)
230 {
231         if (!list_empty(&evt->node)) {
232                 list_del_init(&evt->node);
233                 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
234         }
235 }
236
237 void armada_drm_vbl_event_remove_unlocked(struct armada_crtc *dcrtc,
238         struct armada_vbl_event *evt)
239 {
240         unsigned long flags;
241
242         spin_lock_irqsave(&dcrtc->irq_lock, flags);
243         armada_drm_vbl_event_remove(dcrtc, evt);
244         spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
245 }
246
247 /* These are called under the vbl_lock. */
248 static int armada_drm_enable_vblank(struct drm_device *dev, int crtc)
249 {
250         struct armada_private *priv = dev->dev_private;
251         armada_drm_crtc_enable_irq(priv->dcrtc[crtc], VSYNC_IRQ_ENA);
252         return 0;
253 }
254
255 static void armada_drm_disable_vblank(struct drm_device *dev, int crtc)
256 {
257         struct armada_private *priv = dev->dev_private;
258         armada_drm_crtc_disable_irq(priv->dcrtc[crtc], VSYNC_IRQ_ENA);
259 }
260
261 static irqreturn_t armada_drm_irq_handler(int irq, void *arg)
262 {
263         struct drm_device *dev = arg;
264         struct armada_private *priv = dev->dev_private;
265         struct armada_crtc *dcrtc = priv->dcrtc[0];
266         uint32_t v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
267         irqreturn_t handled = IRQ_NONE;
268
269         /*
270          * This is rediculous - rather than writing bits to clear, we
271          * have to set the actual status register value.  This is racy.
272          */
273         writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
274
275         /* Mask out those interrupts we haven't enabled */
276         v = stat & dcrtc->irq_ena;
277
278         if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
279                 armada_drm_crtc_irq(dcrtc, stat);
280                 handled = IRQ_HANDLED;
281         }
282
283         return handled;
284 }
285
286 static int armada_drm_irq_postinstall(struct drm_device *dev)
287 {
288         struct armada_private *priv = dev->dev_private;
289         struct armada_crtc *dcrtc = priv->dcrtc[0];
290
291         spin_lock_irq(&dev->vbl_lock);
292         writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
293         writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
294         spin_unlock_irq(&dev->vbl_lock);
295
296         return 0;
297 }
298
299 static void armada_drm_irq_uninstall(struct drm_device *dev)
300 {
301         struct armada_private *priv = dev->dev_private;
302         struct armada_crtc *dcrtc = priv->dcrtc[0];
303
304         writel(0, dcrtc->base + LCD_SPU_IRQ_ENA);
305 }
306
307 static struct drm_ioctl_desc armada_ioctls[] = {
308         DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,
309                 DRM_UNLOCKED),
310         DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl,
311                 DRM_UNLOCKED),
312         DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl,
313                 DRM_UNLOCKED),
314 };
315
316 static void armada_drm_lastclose(struct drm_device *dev)
317 {
318         armada_fbdev_lastclose(dev);
319 }
320
321 static const struct file_operations armada_drm_fops = {
322         .owner                  = THIS_MODULE,
323         .llseek                 = no_llseek,
324         .read                   = drm_read,
325         .poll                   = drm_poll,
326         .unlocked_ioctl         = drm_ioctl,
327         .mmap                   = drm_gem_mmap,
328         .open                   = drm_open,
329         .release                = drm_release,
330 };
331
332 static struct drm_driver armada_drm_driver = {
333         .load                   = armada_drm_load,
334         .open                   = NULL,
335         .preclose               = NULL,
336         .postclose              = NULL,
337         .lastclose              = armada_drm_lastclose,
338         .unload                 = armada_drm_unload,
339         .get_vblank_counter     = drm_vblank_count,
340         .enable_vblank          = armada_drm_enable_vblank,
341         .disable_vblank         = armada_drm_disable_vblank,
342         .irq_handler            = armada_drm_irq_handler,
343         .irq_postinstall        = armada_drm_irq_postinstall,
344         .irq_uninstall          = armada_drm_irq_uninstall,
345 #ifdef CONFIG_DEBUG_FS
346         .debugfs_init           = armada_drm_debugfs_init,
347         .debugfs_cleanup        = armada_drm_debugfs_cleanup,
348 #endif
349         .gem_free_object        = armada_gem_free_object,
350         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
351         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
352         .gem_prime_export       = armada_gem_prime_export,
353         .gem_prime_import       = armada_gem_prime_import,
354         .dumb_create            = armada_gem_dumb_create,
355         .dumb_map_offset        = armada_gem_dumb_map_offset,
356         .dumb_destroy           = armada_gem_dumb_destroy,
357         .gem_vm_ops             = &armada_gem_vm_ops,
358         .major                  = 1,
359         .minor                  = 0,
360         .name                   = "armada-drm",
361         .desc                   = "Armada SoC DRM",
362         .date                   = "20120730",
363         .driver_features        = DRIVER_GEM | DRIVER_MODESET |
364                                   DRIVER_HAVE_IRQ | DRIVER_PRIME,
365         .ioctls                 = armada_ioctls,
366         .fops                   = &armada_drm_fops,
367 };
368
369 static int armada_drm_probe(struct platform_device *pdev)
370 {
371         return drm_platform_init(&armada_drm_driver, pdev);
372 }
373
374 static int armada_drm_remove(struct platform_device *pdev)
375 {
376         drm_platform_exit(&armada_drm_driver, pdev);
377         return 0;
378 }
379
380 static const struct platform_device_id armada_drm_platform_ids[] = {
381         {
382                 .name           = "armada-drm",
383                 .driver_data    = (unsigned long)&armada510_ops,
384         }, {
385                 .name           = "armada-510-drm",
386                 .driver_data    = (unsigned long)&armada510_ops,
387         },
388         { },
389 };
390 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
391
392 static struct platform_driver armada_drm_platform_driver = {
393         .probe  = armada_drm_probe,
394         .remove = armada_drm_remove,
395         .driver = {
396                 .name   = "armada-drm",
397                 .owner  = THIS_MODULE,
398         },
399         .id_table = armada_drm_platform_ids,
400 };
401
402 static int __init armada_drm_init(void)
403 {
404         armada_drm_driver.num_ioctls = DRM_ARRAY_SIZE(armada_ioctls);
405         return platform_driver_register(&armada_drm_platform_driver);
406 }
407 module_init(armada_drm_init);
408
409 static void __exit armada_drm_exit(void)
410 {
411         platform_driver_unregister(&armada_drm_platform_driver);
412 }
413 module_exit(armada_drm_exit);
414
415 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
416 MODULE_DESCRIPTION("Armada DRM Driver");
417 MODULE_LICENSE("GPL");
418 MODULE_ALIAS("platform:armada-drm");