armada_drv.c 11 KB

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