rockchip_drm_drv.c 14 KB

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