rockchip_drm_drv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
  3. * Author:Mark Yao <mark.yao@rock-chips.com>
  4. *
  5. * based on exynos_drm_drv.c
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <asm/dma-iommu.h>
  17. #include <drm/drmP.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include <drm/drm_fb_helper.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/of_graph.h>
  23. #include <linux/component.h>
  24. #include "rockchip_drm_drv.h"
  25. #include "rockchip_drm_fb.h"
  26. #include "rockchip_drm_fbdev.h"
  27. #include "rockchip_drm_gem.h"
  28. #define DRIVER_NAME "rockchip"
  29. #define DRIVER_DESC "RockChip Soc DRM"
  30. #define DRIVER_DATE "20140818"
  31. #define DRIVER_MAJOR 1
  32. #define DRIVER_MINOR 0
  33. /*
  34. * Attach a (component) device to the shared drm dma mapping from master drm
  35. * device. This is used by the VOPs to map GEM buffers to a common DMA
  36. * mapping.
  37. */
  38. int rockchip_drm_dma_attach_device(struct drm_device *drm_dev,
  39. struct device *dev)
  40. {
  41. struct dma_iommu_mapping *mapping = drm_dev->dev->archdata.mapping;
  42. int ret;
  43. ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
  44. if (ret)
  45. return ret;
  46. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  47. return arm_iommu_attach_device(dev, mapping);
  48. }
  49. EXPORT_SYMBOL_GPL(rockchip_drm_dma_attach_device);
  50. void rockchip_drm_dma_detach_device(struct drm_device *drm_dev,
  51. struct device *dev)
  52. {
  53. arm_iommu_detach_device(dev);
  54. }
  55. EXPORT_SYMBOL_GPL(rockchip_drm_dma_detach_device);
  56. int rockchip_register_crtc_funcs(struct drm_device *dev,
  57. const struct rockchip_crtc_funcs *crtc_funcs,
  58. int pipe)
  59. {
  60. struct rockchip_drm_private *priv = dev->dev_private;
  61. if (pipe > ROCKCHIP_MAX_CRTC)
  62. return -EINVAL;
  63. priv->crtc_funcs[pipe] = crtc_funcs;
  64. return 0;
  65. }
  66. EXPORT_SYMBOL_GPL(rockchip_register_crtc_funcs);
  67. void rockchip_unregister_crtc_funcs(struct drm_device *dev, int pipe)
  68. {
  69. struct rockchip_drm_private *priv = dev->dev_private;
  70. if (pipe > ROCKCHIP_MAX_CRTC)
  71. return;
  72. priv->crtc_funcs[pipe] = NULL;
  73. }
  74. EXPORT_SYMBOL_GPL(rockchip_unregister_crtc_funcs);
  75. static struct drm_crtc *rockchip_crtc_from_pipe(struct drm_device *drm,
  76. int pipe)
  77. {
  78. struct drm_crtc *crtc;
  79. int i = 0;
  80. list_for_each_entry(crtc, &drm->mode_config.crtc_list, head)
  81. if (i++ == pipe)
  82. return crtc;
  83. return NULL;
  84. }
  85. static int rockchip_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
  86. {
  87. struct rockchip_drm_private *priv = dev->dev_private;
  88. struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
  89. if (crtc && priv->crtc_funcs[pipe] &&
  90. priv->crtc_funcs[pipe]->enable_vblank)
  91. return priv->crtc_funcs[pipe]->enable_vblank(crtc);
  92. return 0;
  93. }
  94. static void rockchip_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
  95. {
  96. struct rockchip_drm_private *priv = dev->dev_private;
  97. struct drm_crtc *crtc = rockchip_crtc_from_pipe(dev, pipe);
  98. if (crtc && priv->crtc_funcs[pipe] &&
  99. priv->crtc_funcs[pipe]->enable_vblank)
  100. priv->crtc_funcs[pipe]->disable_vblank(crtc);
  101. }
  102. static int rockchip_drm_load(struct drm_device *drm_dev, unsigned long flags)
  103. {
  104. struct rockchip_drm_private *private;
  105. struct dma_iommu_mapping *mapping;
  106. struct device *dev = drm_dev->dev;
  107. int ret;
  108. private = devm_kzalloc(drm_dev->dev, sizeof(*private), GFP_KERNEL);
  109. if (!private)
  110. return -ENOMEM;
  111. drm_dev->dev_private = private;
  112. drm_mode_config_init(drm_dev);
  113. rockchip_drm_mode_config_init(drm_dev);
  114. dev->dma_parms = devm_kzalloc(dev, sizeof(*dev->dma_parms),
  115. GFP_KERNEL);
  116. if (!dev->dma_parms) {
  117. ret = -ENOMEM;
  118. goto err_config_cleanup;
  119. }
  120. /* TODO(djkurtz): fetch the mapping start/size from somewhere */
  121. mapping = arm_iommu_create_mapping(&platform_bus_type, 0x00000000,
  122. SZ_2G);
  123. if (IS_ERR(mapping)) {
  124. ret = PTR_ERR(mapping);
  125. goto err_config_cleanup;
  126. }
  127. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  128. if (ret)
  129. goto err_release_mapping;
  130. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  131. ret = arm_iommu_attach_device(dev, mapping);
  132. if (ret)
  133. goto err_release_mapping;
  134. /* Try to bind all sub drivers. */
  135. ret = component_bind_all(dev, drm_dev);
  136. if (ret)
  137. goto err_detach_device;
  138. /* init kms poll for handling hpd */
  139. drm_kms_helper_poll_init(drm_dev);
  140. /*
  141. * enable drm irq mode.
  142. * - with irq_enabled = true, we can use the vblank feature.
  143. */
  144. drm_dev->irq_enabled = true;
  145. ret = drm_vblank_init(drm_dev, ROCKCHIP_MAX_CRTC);
  146. if (ret)
  147. goto err_kms_helper_poll_fini;
  148. /*
  149. * with vblank_disable_allowed = true, vblank interrupt will be disabled
  150. * by drm timer once a current process gives up ownership of
  151. * vblank event.(after drm_vblank_put function is called)
  152. */
  153. drm_dev->vblank_disable_allowed = true;
  154. ret = rockchip_drm_fbdev_init(drm_dev);
  155. if (ret)
  156. goto err_vblank_cleanup;
  157. return 0;
  158. err_vblank_cleanup:
  159. drm_vblank_cleanup(drm_dev);
  160. err_kms_helper_poll_fini:
  161. drm_kms_helper_poll_fini(drm_dev);
  162. component_unbind_all(dev, drm_dev);
  163. err_detach_device:
  164. arm_iommu_detach_device(dev);
  165. err_release_mapping:
  166. arm_iommu_release_mapping(dev->archdata.mapping);
  167. err_config_cleanup:
  168. drm_mode_config_cleanup(drm_dev);
  169. drm_dev->dev_private = NULL;
  170. return ret;
  171. }
  172. static int rockchip_drm_unload(struct drm_device *drm_dev)
  173. {
  174. struct device *dev = drm_dev->dev;
  175. rockchip_drm_fbdev_fini(drm_dev);
  176. drm_vblank_cleanup(drm_dev);
  177. drm_kms_helper_poll_fini(drm_dev);
  178. component_unbind_all(dev, drm_dev);
  179. arm_iommu_detach_device(dev);
  180. arm_iommu_release_mapping(dev->archdata.mapping);
  181. drm_mode_config_cleanup(drm_dev);
  182. drm_dev->dev_private = NULL;
  183. return 0;
  184. }
  185. void rockchip_drm_lastclose(struct drm_device *dev)
  186. {
  187. struct rockchip_drm_private *priv = dev->dev_private;
  188. drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
  189. }
  190. static const struct file_operations rockchip_drm_driver_fops = {
  191. .owner = THIS_MODULE,
  192. .open = drm_open,
  193. .mmap = rockchip_gem_mmap,
  194. .poll = drm_poll,
  195. .read = drm_read,
  196. .unlocked_ioctl = drm_ioctl,
  197. #ifdef CONFIG_COMPAT
  198. .compat_ioctl = drm_compat_ioctl,
  199. #endif
  200. .release = drm_release,
  201. };
  202. const struct vm_operations_struct rockchip_drm_vm_ops = {
  203. .open = drm_gem_vm_open,
  204. .close = drm_gem_vm_close,
  205. };
  206. static struct drm_driver rockchip_drm_driver = {
  207. .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
  208. .load = rockchip_drm_load,
  209. .unload = rockchip_drm_unload,
  210. .lastclose = rockchip_drm_lastclose,
  211. .get_vblank_counter = drm_vblank_count,
  212. .enable_vblank = rockchip_drm_crtc_enable_vblank,
  213. .disable_vblank = rockchip_drm_crtc_disable_vblank,
  214. .gem_vm_ops = &rockchip_drm_vm_ops,
  215. .gem_free_object = rockchip_gem_free_object,
  216. .dumb_create = rockchip_gem_dumb_create,
  217. .dumb_map_offset = rockchip_gem_dumb_map_offset,
  218. .dumb_destroy = drm_gem_dumb_destroy,
  219. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  220. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  221. .gem_prime_import = drm_gem_prime_import,
  222. .gem_prime_export = drm_gem_prime_export,
  223. .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
  224. .gem_prime_vmap = rockchip_gem_prime_vmap,
  225. .gem_prime_vunmap = rockchip_gem_prime_vunmap,
  226. .gem_prime_mmap = rockchip_gem_mmap_buf,
  227. .fops = &rockchip_drm_driver_fops,
  228. .name = DRIVER_NAME,
  229. .desc = DRIVER_DESC,
  230. .date = DRIVER_DATE,
  231. .major = DRIVER_MAJOR,
  232. .minor = DRIVER_MINOR,
  233. };
  234. #ifdef CONFIG_PM_SLEEP
  235. static int rockchip_drm_sys_suspend(struct device *dev)
  236. {
  237. struct drm_device *drm = dev_get_drvdata(dev);
  238. struct drm_connector *connector;
  239. if (!drm)
  240. return 0;
  241. drm_modeset_lock_all(drm);
  242. list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
  243. int old_dpms = connector->dpms;
  244. if (connector->funcs->dpms)
  245. connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
  246. /* Set the old mode back to the connector for resume */
  247. connector->dpms = old_dpms;
  248. }
  249. drm_modeset_unlock_all(drm);
  250. return 0;
  251. }
  252. static int rockchip_drm_sys_resume(struct device *dev)
  253. {
  254. struct drm_device *drm = dev_get_drvdata(dev);
  255. struct drm_connector *connector;
  256. enum drm_connector_status status;
  257. bool changed = false;
  258. if (!drm)
  259. return 0;
  260. drm_modeset_lock_all(drm);
  261. list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
  262. int desired_mode = connector->dpms;
  263. /*
  264. * at suspend time, we save dpms to connector->dpms,
  265. * restore the old_dpms, and at current time, the connector
  266. * dpms status must be DRM_MODE_DPMS_OFF.
  267. */
  268. connector->dpms = DRM_MODE_DPMS_OFF;
  269. /*
  270. * If the connector has been disconnected during suspend,
  271. * disconnect it from the encoder and leave it off. We'll notify
  272. * userspace at the end.
  273. */
  274. if (desired_mode == DRM_MODE_DPMS_ON) {
  275. status = connector->funcs->detect(connector, true);
  276. if (status == connector_status_disconnected) {
  277. connector->encoder = NULL;
  278. connector->status = status;
  279. changed = true;
  280. continue;
  281. }
  282. }
  283. if (connector->funcs->dpms)
  284. connector->funcs->dpms(connector, desired_mode);
  285. }
  286. drm_modeset_unlock_all(drm);
  287. drm_helper_resume_force_mode(drm);
  288. if (changed)
  289. drm_kms_helper_hotplug_event(drm);
  290. return 0;
  291. }
  292. #endif
  293. static const struct dev_pm_ops rockchip_drm_pm_ops = {
  294. SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
  295. rockchip_drm_sys_resume)
  296. };
  297. /*
  298. * @node: device tree node containing encoder input ports
  299. * @encoder: drm_encoder
  300. */
  301. int rockchip_drm_encoder_get_mux_id(struct device_node *node,
  302. struct drm_encoder *encoder)
  303. {
  304. struct device_node *ep = NULL;
  305. struct drm_crtc *crtc = encoder->crtc;
  306. struct of_endpoint endpoint;
  307. struct device_node *port;
  308. int ret;
  309. if (!node || !crtc)
  310. return -EINVAL;
  311. do {
  312. ep = of_graph_get_next_endpoint(node, ep);
  313. if (!ep)
  314. break;
  315. port = of_graph_get_remote_port(ep);
  316. of_node_put(port);
  317. if (port == crtc->port) {
  318. ret = of_graph_parse_endpoint(ep, &endpoint);
  319. return ret ?: endpoint.id;
  320. }
  321. } while (ep);
  322. return -EINVAL;
  323. }
  324. static int compare_of(struct device *dev, void *data)
  325. {
  326. struct device_node *np = data;
  327. return dev->of_node == np;
  328. }
  329. static void rockchip_add_endpoints(struct device *dev,
  330. struct component_match **match,
  331. struct device_node *port)
  332. {
  333. struct device_node *ep, *remote;
  334. for_each_child_of_node(port, ep) {
  335. remote = of_graph_get_remote_port_parent(ep);
  336. if (!remote || !of_device_is_available(remote)) {
  337. of_node_put(remote);
  338. continue;
  339. } else if (!of_device_is_available(remote->parent)) {
  340. dev_warn(dev, "parent device of %s is not available\n",
  341. remote->full_name);
  342. of_node_put(remote);
  343. continue;
  344. }
  345. component_match_add(dev, match, compare_of, remote);
  346. of_node_put(remote);
  347. }
  348. }
  349. static int rockchip_drm_bind(struct device *dev)
  350. {
  351. struct drm_device *drm;
  352. int ret;
  353. drm = drm_dev_alloc(&rockchip_drm_driver, dev);
  354. if (!drm)
  355. return -ENOMEM;
  356. ret = drm_dev_set_unique(drm, "%s", dev_name(dev));
  357. if (ret)
  358. goto err_free;
  359. ret = drm_dev_register(drm, 0);
  360. if (ret)
  361. goto err_free;
  362. dev_set_drvdata(dev, drm);
  363. return 0;
  364. err_free:
  365. drm_dev_unref(drm);
  366. return ret;
  367. }
  368. static void rockchip_drm_unbind(struct device *dev)
  369. {
  370. struct drm_device *drm = dev_get_drvdata(dev);
  371. drm_dev_unregister(drm);
  372. drm_dev_unref(drm);
  373. dev_set_drvdata(dev, NULL);
  374. }
  375. static const struct component_master_ops rockchip_drm_ops = {
  376. .bind = rockchip_drm_bind,
  377. .unbind = rockchip_drm_unbind,
  378. };
  379. static int rockchip_drm_platform_probe(struct platform_device *pdev)
  380. {
  381. struct device *dev = &pdev->dev;
  382. struct component_match *match = NULL;
  383. struct device_node *np = dev->of_node;
  384. struct device_node *port;
  385. int i;
  386. if (!np)
  387. return -ENODEV;
  388. /*
  389. * Bind the crtc ports first, so that
  390. * drm_of_find_possible_crtcs called from encoder .bind callbacks
  391. * works as expected.
  392. */
  393. for (i = 0;; i++) {
  394. port = of_parse_phandle(np, "ports", i);
  395. if (!port)
  396. break;
  397. if (!of_device_is_available(port->parent)) {
  398. of_node_put(port);
  399. continue;
  400. }
  401. component_match_add(dev, &match, compare_of, port->parent);
  402. of_node_put(port);
  403. }
  404. if (i == 0) {
  405. dev_err(dev, "missing 'ports' property\n");
  406. return -ENODEV;
  407. }
  408. if (!match) {
  409. dev_err(dev, "No available vop found for display-subsystem.\n");
  410. return -ENODEV;
  411. }
  412. /*
  413. * For each bound crtc, bind the encoders attached to its
  414. * remote endpoint.
  415. */
  416. for (i = 0;; i++) {
  417. port = of_parse_phandle(np, "ports", i);
  418. if (!port)
  419. break;
  420. if (!of_device_is_available(port->parent)) {
  421. of_node_put(port);
  422. continue;
  423. }
  424. rockchip_add_endpoints(dev, &match, port);
  425. of_node_put(port);
  426. }
  427. return component_master_add_with_match(dev, &rockchip_drm_ops, match);
  428. }
  429. static int rockchip_drm_platform_remove(struct platform_device *pdev)
  430. {
  431. component_master_del(&pdev->dev, &rockchip_drm_ops);
  432. return 0;
  433. }
  434. static const struct of_device_id rockchip_drm_dt_ids[] = {
  435. { .compatible = "rockchip,display-subsystem", },
  436. { /* sentinel */ },
  437. };
  438. MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
  439. static struct platform_driver rockchip_drm_platform_driver = {
  440. .probe = rockchip_drm_platform_probe,
  441. .remove = rockchip_drm_platform_remove,
  442. .driver = {
  443. .owner = THIS_MODULE,
  444. .name = "rockchip-drm",
  445. .of_match_table = rockchip_drm_dt_ids,
  446. .pm = &rockchip_drm_pm_ops,
  447. },
  448. };
  449. module_platform_driver(rockchip_drm_platform_driver);
  450. MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
  451. MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
  452. MODULE_LICENSE("GPL v2");