malidp_drv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
  3. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * ARM Mali DP500/DP550/DP650 KMS/DRM driver
  11. */
  12. #include <linux/module.h>
  13. #include <linux/clk.h>
  14. #include <linux/component.h>
  15. #include <linux/of_device.h>
  16. #include <linux/of_graph.h>
  17. #include <linux/of_reserved_mem.h>
  18. #include <drm/drmP.h>
  19. #include <drm/drm_atomic.h>
  20. #include <drm/drm_atomic_helper.h>
  21. #include <drm/drm_crtc.h>
  22. #include <drm/drm_crtc_helper.h>
  23. #include <drm/drm_fb_helper.h>
  24. #include <drm/drm_fb_cma_helper.h>
  25. #include <drm/drm_gem_cma_helper.h>
  26. #include <drm/drm_of.h>
  27. #include "malidp_drv.h"
  28. #include "malidp_regs.h"
  29. #include "malidp_hw.h"
  30. #define MALIDP_CONF_VALID_TIMEOUT 250
  31. /*
  32. * set the "config valid" bit and wait until the hardware acts on it
  33. */
  34. static int malidp_set_and_wait_config_valid(struct drm_device *drm)
  35. {
  36. struct malidp_drm *malidp = drm->dev_private;
  37. struct malidp_hw_device *hwdev = malidp->dev;
  38. int ret;
  39. atomic_set(&malidp->config_valid, 0);
  40. hwdev->set_config_valid(hwdev);
  41. /* don't wait for config_valid flag if we are in config mode */
  42. if (hwdev->in_config_mode(hwdev))
  43. return 0;
  44. ret = wait_event_interruptible_timeout(malidp->wq,
  45. atomic_read(&malidp->config_valid) == 1,
  46. msecs_to_jiffies(MALIDP_CONF_VALID_TIMEOUT));
  47. return (ret > 0) ? 0 : -ETIMEDOUT;
  48. }
  49. static void malidp_output_poll_changed(struct drm_device *drm)
  50. {
  51. struct malidp_drm *malidp = drm->dev_private;
  52. drm_fbdev_cma_hotplug_event(malidp->fbdev);
  53. }
  54. static void malidp_atomic_commit_hw_done(struct drm_atomic_state *state)
  55. {
  56. struct drm_pending_vblank_event *event;
  57. struct drm_device *drm = state->dev;
  58. struct malidp_drm *malidp = drm->dev_private;
  59. int ret = malidp_set_and_wait_config_valid(drm);
  60. if (ret)
  61. DRM_DEBUG_DRIVER("timed out waiting for updated configuration\n");
  62. event = malidp->crtc.state->event;
  63. if (event) {
  64. malidp->crtc.state->event = NULL;
  65. spin_lock_irq(&drm->event_lock);
  66. if (drm_crtc_vblank_get(&malidp->crtc) == 0)
  67. drm_crtc_arm_vblank_event(&malidp->crtc, event);
  68. else
  69. drm_crtc_send_vblank_event(&malidp->crtc, event);
  70. spin_unlock_irq(&drm->event_lock);
  71. }
  72. drm_atomic_helper_commit_hw_done(state);
  73. }
  74. static void malidp_atomic_commit_tail(struct drm_atomic_state *state)
  75. {
  76. struct drm_device *drm = state->dev;
  77. drm_atomic_helper_commit_modeset_disables(drm, state);
  78. drm_atomic_helper_commit_modeset_enables(drm, state);
  79. drm_atomic_helper_commit_planes(drm, state, 0);
  80. malidp_atomic_commit_hw_done(state);
  81. drm_atomic_helper_wait_for_vblanks(drm, state);
  82. drm_atomic_helper_cleanup_planes(drm, state);
  83. }
  84. static struct drm_mode_config_helper_funcs malidp_mode_config_helpers = {
  85. .atomic_commit_tail = malidp_atomic_commit_tail,
  86. };
  87. static const struct drm_mode_config_funcs malidp_mode_config_funcs = {
  88. .fb_create = drm_fb_cma_create,
  89. .output_poll_changed = malidp_output_poll_changed,
  90. .atomic_check = drm_atomic_helper_check,
  91. .atomic_commit = drm_atomic_helper_commit,
  92. };
  93. static int malidp_enable_vblank(struct drm_device *drm, unsigned int crtc)
  94. {
  95. struct malidp_drm *malidp = drm->dev_private;
  96. struct malidp_hw_device *hwdev = malidp->dev;
  97. malidp_hw_enable_irq(hwdev, MALIDP_DE_BLOCK,
  98. hwdev->map.de_irq_map.vsync_irq);
  99. return 0;
  100. }
  101. static void malidp_disable_vblank(struct drm_device *drm, unsigned int pipe)
  102. {
  103. struct malidp_drm *malidp = drm->dev_private;
  104. struct malidp_hw_device *hwdev = malidp->dev;
  105. malidp_hw_disable_irq(hwdev, MALIDP_DE_BLOCK,
  106. hwdev->map.de_irq_map.vsync_irq);
  107. }
  108. static int malidp_init(struct drm_device *drm)
  109. {
  110. int ret;
  111. struct malidp_drm *malidp = drm->dev_private;
  112. struct malidp_hw_device *hwdev = malidp->dev;
  113. drm_mode_config_init(drm);
  114. drm->mode_config.min_width = hwdev->min_line_size;
  115. drm->mode_config.min_height = hwdev->min_line_size;
  116. drm->mode_config.max_width = hwdev->max_line_size;
  117. drm->mode_config.max_height = hwdev->max_line_size;
  118. drm->mode_config.funcs = &malidp_mode_config_funcs;
  119. drm->mode_config.helper_private = &malidp_mode_config_helpers;
  120. ret = malidp_crtc_init(drm);
  121. if (ret) {
  122. drm_mode_config_cleanup(drm);
  123. return ret;
  124. }
  125. return 0;
  126. }
  127. static void malidp_fini(struct drm_device *drm)
  128. {
  129. malidp_de_planes_destroy(drm);
  130. drm_mode_config_cleanup(drm);
  131. }
  132. static int malidp_irq_init(struct platform_device *pdev)
  133. {
  134. int irq_de, irq_se, ret = 0;
  135. struct drm_device *drm = dev_get_drvdata(&pdev->dev);
  136. /* fetch the interrupts from DT */
  137. irq_de = platform_get_irq_byname(pdev, "DE");
  138. if (irq_de < 0) {
  139. DRM_ERROR("no 'DE' IRQ specified!\n");
  140. return irq_de;
  141. }
  142. irq_se = platform_get_irq_byname(pdev, "SE");
  143. if (irq_se < 0) {
  144. DRM_ERROR("no 'SE' IRQ specified!\n");
  145. return irq_se;
  146. }
  147. ret = malidp_de_irq_init(drm, irq_de);
  148. if (ret)
  149. return ret;
  150. ret = malidp_se_irq_init(drm, irq_se);
  151. if (ret) {
  152. malidp_de_irq_fini(drm);
  153. return ret;
  154. }
  155. return 0;
  156. }
  157. static void malidp_lastclose(struct drm_device *drm)
  158. {
  159. struct malidp_drm *malidp = drm->dev_private;
  160. drm_fbdev_cma_restore_mode(malidp->fbdev);
  161. }
  162. static const struct file_operations fops = {
  163. .owner = THIS_MODULE,
  164. .open = drm_open,
  165. .release = drm_release,
  166. .unlocked_ioctl = drm_ioctl,
  167. .compat_ioctl = drm_compat_ioctl,
  168. .poll = drm_poll,
  169. .read = drm_read,
  170. .llseek = noop_llseek,
  171. .mmap = drm_gem_cma_mmap,
  172. };
  173. static struct drm_driver malidp_driver = {
  174. .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC |
  175. DRIVER_PRIME,
  176. .lastclose = malidp_lastclose,
  177. .get_vblank_counter = drm_vblank_no_hw_counter,
  178. .enable_vblank = malidp_enable_vblank,
  179. .disable_vblank = malidp_disable_vblank,
  180. .gem_free_object_unlocked = drm_gem_cma_free_object,
  181. .gem_vm_ops = &drm_gem_cma_vm_ops,
  182. .dumb_create = drm_gem_cma_dumb_create,
  183. .dumb_map_offset = drm_gem_cma_dumb_map_offset,
  184. .dumb_destroy = drm_gem_dumb_destroy,
  185. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  186. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  187. .gem_prime_export = drm_gem_prime_export,
  188. .gem_prime_import = drm_gem_prime_import,
  189. .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
  190. .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
  191. .gem_prime_vmap = drm_gem_cma_prime_vmap,
  192. .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
  193. .gem_prime_mmap = drm_gem_cma_prime_mmap,
  194. .fops = &fops,
  195. .name = "mali-dp",
  196. .desc = "ARM Mali Display Processor driver",
  197. .date = "20160106",
  198. .major = 1,
  199. .minor = 0,
  200. };
  201. static const struct of_device_id malidp_drm_of_match[] = {
  202. {
  203. .compatible = "arm,mali-dp500",
  204. .data = &malidp_device[MALIDP_500]
  205. },
  206. {
  207. .compatible = "arm,mali-dp550",
  208. .data = &malidp_device[MALIDP_550]
  209. },
  210. {
  211. .compatible = "arm,mali-dp650",
  212. .data = &malidp_device[MALIDP_650]
  213. },
  214. {},
  215. };
  216. MODULE_DEVICE_TABLE(of, malidp_drm_of_match);
  217. #define MAX_OUTPUT_CHANNELS 3
  218. static int malidp_bind(struct device *dev)
  219. {
  220. struct resource *res;
  221. struct drm_device *drm;
  222. struct device_node *ep;
  223. struct malidp_drm *malidp;
  224. struct malidp_hw_device *hwdev;
  225. struct platform_device *pdev = to_platform_device(dev);
  226. /* number of lines for the R, G and B output */
  227. u8 output_width[MAX_OUTPUT_CHANNELS];
  228. int ret = 0, i;
  229. u32 version, out_depth = 0;
  230. malidp = devm_kzalloc(dev, sizeof(*malidp), GFP_KERNEL);
  231. if (!malidp)
  232. return -ENOMEM;
  233. hwdev = devm_kzalloc(dev, sizeof(*hwdev), GFP_KERNEL);
  234. if (!hwdev)
  235. return -ENOMEM;
  236. /*
  237. * copy the associated data from malidp_drm_of_match to avoid
  238. * having to keep a reference to the OF node after binding
  239. */
  240. memcpy(hwdev, of_device_get_match_data(dev), sizeof(*hwdev));
  241. malidp->dev = hwdev;
  242. INIT_LIST_HEAD(&malidp->event_list);
  243. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  244. hwdev->regs = devm_ioremap_resource(dev, res);
  245. if (IS_ERR(hwdev->regs))
  246. return PTR_ERR(hwdev->regs);
  247. hwdev->pclk = devm_clk_get(dev, "pclk");
  248. if (IS_ERR(hwdev->pclk))
  249. return PTR_ERR(hwdev->pclk);
  250. hwdev->aclk = devm_clk_get(dev, "aclk");
  251. if (IS_ERR(hwdev->aclk))
  252. return PTR_ERR(hwdev->aclk);
  253. hwdev->mclk = devm_clk_get(dev, "mclk");
  254. if (IS_ERR(hwdev->mclk))
  255. return PTR_ERR(hwdev->mclk);
  256. hwdev->pxlclk = devm_clk_get(dev, "pxlclk");
  257. if (IS_ERR(hwdev->pxlclk))
  258. return PTR_ERR(hwdev->pxlclk);
  259. /* Get the optional framebuffer memory resource */
  260. ret = of_reserved_mem_device_init(dev);
  261. if (ret && ret != -ENODEV)
  262. return ret;
  263. drm = drm_dev_alloc(&malidp_driver, dev);
  264. if (IS_ERR(drm)) {
  265. ret = PTR_ERR(drm);
  266. goto alloc_fail;
  267. }
  268. /* Enable APB clock in order to get access to the registers */
  269. clk_prepare_enable(hwdev->pclk);
  270. /*
  271. * Enable AXI clock and main clock so that prefetch can start once
  272. * the registers are set
  273. */
  274. clk_prepare_enable(hwdev->aclk);
  275. clk_prepare_enable(hwdev->mclk);
  276. ret = hwdev->query_hw(hwdev);
  277. if (ret) {
  278. DRM_ERROR("Invalid HW configuration\n");
  279. goto query_hw_fail;
  280. }
  281. version = malidp_hw_read(hwdev, hwdev->map.dc_base + MALIDP_DE_CORE_ID);
  282. DRM_INFO("found ARM Mali-DP%3x version r%dp%d\n", version >> 16,
  283. (version >> 12) & 0xf, (version >> 8) & 0xf);
  284. /* set the number of lines used for output of RGB data */
  285. ret = of_property_read_u8_array(dev->of_node,
  286. "arm,malidp-output-port-lines",
  287. output_width, MAX_OUTPUT_CHANNELS);
  288. if (ret)
  289. goto query_hw_fail;
  290. for (i = 0; i < MAX_OUTPUT_CHANNELS; i++)
  291. out_depth = (out_depth << 8) | (output_width[i] & 0xf);
  292. malidp_hw_write(hwdev, out_depth, hwdev->map.out_depth_base);
  293. drm->dev_private = malidp;
  294. dev_set_drvdata(dev, drm);
  295. atomic_set(&malidp->config_valid, 0);
  296. init_waitqueue_head(&malidp->wq);
  297. ret = malidp_init(drm);
  298. if (ret < 0)
  299. goto init_fail;
  300. /* Set the CRTC's port so that the encoder component can find it */
  301. ep = of_graph_get_next_endpoint(dev->of_node, NULL);
  302. if (!ep) {
  303. ret = -EINVAL;
  304. goto port_fail;
  305. }
  306. malidp->crtc.port = of_get_next_parent(ep);
  307. ret = component_bind_all(dev, drm);
  308. if (ret) {
  309. DRM_ERROR("Failed to bind all components\n");
  310. goto bind_fail;
  311. }
  312. ret = malidp_irq_init(pdev);
  313. if (ret < 0)
  314. goto irq_init_fail;
  315. drm->irq_enabled = true;
  316. ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
  317. if (ret < 0) {
  318. DRM_ERROR("failed to initialise vblank\n");
  319. goto vblank_fail;
  320. }
  321. drm_mode_config_reset(drm);
  322. malidp->fbdev = drm_fbdev_cma_init(drm, 32, drm->mode_config.num_crtc,
  323. drm->mode_config.num_connector);
  324. if (IS_ERR(malidp->fbdev)) {
  325. ret = PTR_ERR(malidp->fbdev);
  326. malidp->fbdev = NULL;
  327. goto fbdev_fail;
  328. }
  329. drm_kms_helper_poll_init(drm);
  330. ret = drm_dev_register(drm, 0);
  331. if (ret)
  332. goto register_fail;
  333. return 0;
  334. register_fail:
  335. if (malidp->fbdev) {
  336. drm_fbdev_cma_fini(malidp->fbdev);
  337. malidp->fbdev = NULL;
  338. }
  339. fbdev_fail:
  340. drm_vblank_cleanup(drm);
  341. vblank_fail:
  342. malidp_se_irq_fini(drm);
  343. malidp_de_irq_fini(drm);
  344. drm->irq_enabled = false;
  345. irq_init_fail:
  346. component_unbind_all(dev, drm);
  347. bind_fail:
  348. of_node_put(malidp->crtc.port);
  349. malidp->crtc.port = NULL;
  350. port_fail:
  351. malidp_fini(drm);
  352. init_fail:
  353. drm->dev_private = NULL;
  354. dev_set_drvdata(dev, NULL);
  355. query_hw_fail:
  356. clk_disable_unprepare(hwdev->mclk);
  357. clk_disable_unprepare(hwdev->aclk);
  358. clk_disable_unprepare(hwdev->pclk);
  359. drm_dev_unref(drm);
  360. alloc_fail:
  361. of_reserved_mem_device_release(dev);
  362. return ret;
  363. }
  364. static void malidp_unbind(struct device *dev)
  365. {
  366. struct drm_device *drm = dev_get_drvdata(dev);
  367. struct malidp_drm *malidp = drm->dev_private;
  368. struct malidp_hw_device *hwdev = malidp->dev;
  369. drm_dev_unregister(drm);
  370. if (malidp->fbdev) {
  371. drm_fbdev_cma_fini(malidp->fbdev);
  372. malidp->fbdev = NULL;
  373. }
  374. drm_kms_helper_poll_fini(drm);
  375. malidp_se_irq_fini(drm);
  376. malidp_de_irq_fini(drm);
  377. drm_vblank_cleanup(drm);
  378. component_unbind_all(dev, drm);
  379. of_node_put(malidp->crtc.port);
  380. malidp->crtc.port = NULL;
  381. malidp_fini(drm);
  382. drm->dev_private = NULL;
  383. dev_set_drvdata(dev, NULL);
  384. clk_disable_unprepare(hwdev->mclk);
  385. clk_disable_unprepare(hwdev->aclk);
  386. clk_disable_unprepare(hwdev->pclk);
  387. drm_dev_unref(drm);
  388. of_reserved_mem_device_release(dev);
  389. }
  390. static const struct component_master_ops malidp_master_ops = {
  391. .bind = malidp_bind,
  392. .unbind = malidp_unbind,
  393. };
  394. static int malidp_compare_dev(struct device *dev, void *data)
  395. {
  396. struct device_node *np = data;
  397. return dev->of_node == np;
  398. }
  399. static int malidp_platform_probe(struct platform_device *pdev)
  400. {
  401. struct device_node *port, *ep;
  402. struct component_match *match = NULL;
  403. if (!pdev->dev.of_node)
  404. return -ENODEV;
  405. /* there is only one output port inside each device, find it */
  406. ep = of_graph_get_next_endpoint(pdev->dev.of_node, NULL);
  407. if (!ep)
  408. return -ENODEV;
  409. if (!of_device_is_available(ep)) {
  410. of_node_put(ep);
  411. return -ENODEV;
  412. }
  413. /* add the remote encoder port as component */
  414. port = of_graph_get_remote_port_parent(ep);
  415. of_node_put(ep);
  416. if (!port || !of_device_is_available(port)) {
  417. of_node_put(port);
  418. return -EAGAIN;
  419. }
  420. drm_of_component_match_add(&pdev->dev, &match, malidp_compare_dev,
  421. port);
  422. of_node_put(port);
  423. return component_master_add_with_match(&pdev->dev, &malidp_master_ops,
  424. match);
  425. }
  426. static int malidp_platform_remove(struct platform_device *pdev)
  427. {
  428. component_master_del(&pdev->dev, &malidp_master_ops);
  429. return 0;
  430. }
  431. static struct platform_driver malidp_platform_driver = {
  432. .probe = malidp_platform_probe,
  433. .remove = malidp_platform_remove,
  434. .driver = {
  435. .name = "mali-dp",
  436. .of_match_table = malidp_drm_of_match,
  437. },
  438. };
  439. module_platform_driver(malidp_platform_driver);
  440. MODULE_AUTHOR("Liviu Dudau <Liviu.Dudau@arm.com>");
  441. MODULE_DESCRIPTION("ARM Mali DP DRM driver");
  442. MODULE_LICENSE("GPL v2");