rockchip_drm_drv.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. void rockchip_drm_lastclose(struct drm_device *dev)
  207. {
  208. struct rockchip_drm_private *priv = dev->dev_private;
  209. drm_fb_helper_restore_fbdev_mode_unlocked(&priv->fbdev_helper);
  210. }
  211. static const struct file_operations rockchip_drm_driver_fops = {
  212. .owner = THIS_MODULE,
  213. .open = drm_open,
  214. .mmap = rockchip_gem_mmap,
  215. .poll = drm_poll,
  216. .read = drm_read,
  217. .unlocked_ioctl = drm_ioctl,
  218. #ifdef CONFIG_COMPAT
  219. .compat_ioctl = drm_compat_ioctl,
  220. #endif
  221. .release = drm_release,
  222. };
  223. const struct vm_operations_struct rockchip_drm_vm_ops = {
  224. .open = drm_gem_vm_open,
  225. .close = drm_gem_vm_close,
  226. };
  227. static struct drm_driver rockchip_drm_driver = {
  228. .driver_features = DRIVER_MODESET | DRIVER_GEM |
  229. DRIVER_PRIME | DRIVER_ATOMIC,
  230. .load = rockchip_drm_load,
  231. .unload = rockchip_drm_unload,
  232. .lastclose = rockchip_drm_lastclose,
  233. .get_vblank_counter = drm_vblank_no_hw_counter,
  234. .enable_vblank = rockchip_drm_crtc_enable_vblank,
  235. .disable_vblank = rockchip_drm_crtc_disable_vblank,
  236. .gem_vm_ops = &rockchip_drm_vm_ops,
  237. .gem_free_object = rockchip_gem_free_object,
  238. .dumb_create = rockchip_gem_dumb_create,
  239. .dumb_map_offset = rockchip_gem_dumb_map_offset,
  240. .dumb_destroy = drm_gem_dumb_destroy,
  241. .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
  242. .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
  243. .gem_prime_import = drm_gem_prime_import,
  244. .gem_prime_export = drm_gem_prime_export,
  245. .gem_prime_get_sg_table = rockchip_gem_prime_get_sg_table,
  246. .gem_prime_vmap = rockchip_gem_prime_vmap,
  247. .gem_prime_vunmap = rockchip_gem_prime_vunmap,
  248. .gem_prime_mmap = rockchip_gem_mmap_buf,
  249. .fops = &rockchip_drm_driver_fops,
  250. .name = DRIVER_NAME,
  251. .desc = DRIVER_DESC,
  252. .date = DRIVER_DATE,
  253. .major = DRIVER_MAJOR,
  254. .minor = DRIVER_MINOR,
  255. };
  256. #ifdef CONFIG_PM_SLEEP
  257. static int rockchip_drm_sys_suspend(struct device *dev)
  258. {
  259. struct drm_device *drm = dev_get_drvdata(dev);
  260. struct drm_connector *connector;
  261. if (!drm)
  262. return 0;
  263. drm_modeset_lock_all(drm);
  264. list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
  265. int old_dpms = connector->dpms;
  266. if (connector->funcs->dpms)
  267. connector->funcs->dpms(connector, DRM_MODE_DPMS_OFF);
  268. /* Set the old mode back to the connector for resume */
  269. connector->dpms = old_dpms;
  270. }
  271. drm_modeset_unlock_all(drm);
  272. return 0;
  273. }
  274. static int rockchip_drm_sys_resume(struct device *dev)
  275. {
  276. struct drm_device *drm = dev_get_drvdata(dev);
  277. struct drm_connector *connector;
  278. enum drm_connector_status status;
  279. bool changed = false;
  280. if (!drm)
  281. return 0;
  282. drm_modeset_lock_all(drm);
  283. list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
  284. int desired_mode = connector->dpms;
  285. /*
  286. * at suspend time, we save dpms to connector->dpms,
  287. * restore the old_dpms, and at current time, the connector
  288. * dpms status must be DRM_MODE_DPMS_OFF.
  289. */
  290. connector->dpms = DRM_MODE_DPMS_OFF;
  291. /*
  292. * If the connector has been disconnected during suspend,
  293. * disconnect it from the encoder and leave it off. We'll notify
  294. * userspace at the end.
  295. */
  296. if (desired_mode == DRM_MODE_DPMS_ON) {
  297. status = connector->funcs->detect(connector, true);
  298. if (status == connector_status_disconnected) {
  299. connector->encoder = NULL;
  300. connector->status = status;
  301. changed = true;
  302. continue;
  303. }
  304. }
  305. if (connector->funcs->dpms)
  306. connector->funcs->dpms(connector, desired_mode);
  307. }
  308. drm_modeset_unlock_all(drm);
  309. drm_helper_resume_force_mode(drm);
  310. if (changed)
  311. drm_kms_helper_hotplug_event(drm);
  312. return 0;
  313. }
  314. #endif
  315. static const struct dev_pm_ops rockchip_drm_pm_ops = {
  316. SET_SYSTEM_SLEEP_PM_OPS(rockchip_drm_sys_suspend,
  317. rockchip_drm_sys_resume)
  318. };
  319. static int compare_of(struct device *dev, void *data)
  320. {
  321. struct device_node *np = data;
  322. return dev->of_node == np;
  323. }
  324. static void rockchip_add_endpoints(struct device *dev,
  325. struct component_match **match,
  326. struct device_node *port)
  327. {
  328. struct device_node *ep, *remote;
  329. for_each_child_of_node(port, ep) {
  330. remote = of_graph_get_remote_port_parent(ep);
  331. if (!remote || !of_device_is_available(remote)) {
  332. of_node_put(remote);
  333. continue;
  334. } else if (!of_device_is_available(remote->parent)) {
  335. dev_warn(dev, "parent device of %s is not available\n",
  336. remote->full_name);
  337. of_node_put(remote);
  338. continue;
  339. }
  340. component_match_add(dev, match, compare_of, remote);
  341. of_node_put(remote);
  342. }
  343. }
  344. static int rockchip_drm_bind(struct device *dev)
  345. {
  346. struct drm_device *drm;
  347. int ret;
  348. drm = drm_dev_alloc(&rockchip_drm_driver, dev);
  349. if (!drm)
  350. return -ENOMEM;
  351. ret = drm_dev_register(drm, 0);
  352. if (ret)
  353. goto err_free;
  354. dev_set_drvdata(dev, drm);
  355. return 0;
  356. err_free:
  357. drm_dev_unref(drm);
  358. return ret;
  359. }
  360. static void rockchip_drm_unbind(struct device *dev)
  361. {
  362. struct drm_device *drm = dev_get_drvdata(dev);
  363. drm_dev_unregister(drm);
  364. drm_dev_unref(drm);
  365. dev_set_drvdata(dev, NULL);
  366. }
  367. static const struct component_master_ops rockchip_drm_ops = {
  368. .bind = rockchip_drm_bind,
  369. .unbind = rockchip_drm_unbind,
  370. };
  371. static int rockchip_drm_platform_probe(struct platform_device *pdev)
  372. {
  373. struct device *dev = &pdev->dev;
  374. struct component_match *match = NULL;
  375. struct device_node *np = dev->of_node;
  376. struct device_node *port;
  377. int i;
  378. if (!np)
  379. return -ENODEV;
  380. /*
  381. * Bind the crtc ports first, so that
  382. * drm_of_find_possible_crtcs called from encoder .bind callbacks
  383. * works as expected.
  384. */
  385. for (i = 0;; i++) {
  386. port = of_parse_phandle(np, "ports", i);
  387. if (!port)
  388. break;
  389. if (!of_device_is_available(port->parent)) {
  390. of_node_put(port);
  391. continue;
  392. }
  393. component_match_add(dev, &match, compare_of, port->parent);
  394. of_node_put(port);
  395. }
  396. if (i == 0) {
  397. dev_err(dev, "missing 'ports' property\n");
  398. return -ENODEV;
  399. }
  400. if (!match) {
  401. dev_err(dev, "No available vop found for display-subsystem.\n");
  402. return -ENODEV;
  403. }
  404. /*
  405. * For each bound crtc, bind the encoders attached to its
  406. * remote endpoint.
  407. */
  408. for (i = 0;; i++) {
  409. port = of_parse_phandle(np, "ports", i);
  410. if (!port)
  411. break;
  412. if (!of_device_is_available(port->parent)) {
  413. of_node_put(port);
  414. continue;
  415. }
  416. rockchip_add_endpoints(dev, &match, port);
  417. of_node_put(port);
  418. }
  419. return component_master_add_with_match(dev, &rockchip_drm_ops, match);
  420. }
  421. static int rockchip_drm_platform_remove(struct platform_device *pdev)
  422. {
  423. component_master_del(&pdev->dev, &rockchip_drm_ops);
  424. return 0;
  425. }
  426. static const struct of_device_id rockchip_drm_dt_ids[] = {
  427. { .compatible = "rockchip,display-subsystem", },
  428. { /* sentinel */ },
  429. };
  430. MODULE_DEVICE_TABLE(of, rockchip_drm_dt_ids);
  431. static struct platform_driver rockchip_drm_platform_driver = {
  432. .probe = rockchip_drm_platform_probe,
  433. .remove = rockchip_drm_platform_remove,
  434. .driver = {
  435. .name = "rockchip-drm",
  436. .of_match_table = rockchip_drm_dt_ids,
  437. .pm = &rockchip_drm_pm_ops,
  438. },
  439. };
  440. module_platform_driver(rockchip_drm_platform_driver);
  441. MODULE_AUTHOR("Mark Yao <mark.yao@rock-chips.com>");
  442. MODULE_DESCRIPTION("ROCKCHIP DRM Driver");
  443. MODULE_LICENSE("GPL v2");