rockchip_drm_drv.c 14 KB

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